Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * s390 CCW Assignment Support |
| 3 | * |
| 4 | * Copyright 2017 IBM Corp |
| 5 | * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> |
| 6 | * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> |
| 7 | * Pierre Morel <pmorel@linux.vnet.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 |
| 10 | * or (at your option) any later version. See the COPYING file in the |
| 11 | * top-level directory. |
| 12 | */ |
Markus Armbruster | d8e39b7 | 2018-02-01 12:18:28 +0100 | [diff] [blame] | 13 | |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Markus Armbruster | d8e39b7 | 2018-02-01 12:18:28 +0100 | [diff] [blame] | 15 | #include <libgen.h> |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 16 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 17 | #include "qemu/module.h" |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 18 | #include "hw/s390x/css.h" |
| 19 | #include "hw/s390x/css-bridge.h" |
| 20 | #include "hw/s390x/s390-ccw.h" |
Markus Armbruster | 2f780b6 | 2019-08-12 07:23:58 +0200 | [diff] [blame] | 21 | #include "sysemu/sysemu.h" |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 22 | |
Halil Pasic | 66dc50f | 2017-10-17 16:04:49 +0200 | [diff] [blame] | 23 | IOInstEnding s390_ccw_cmd_request(SubchDev *sch) |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 24 | { |
Halil Pasic | 66dc50f | 2017-10-17 16:04:49 +0200 | [diff] [blame] | 25 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data); |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 26 | |
Halil Pasic | 66dc50f | 2017-10-17 16:04:49 +0200 | [diff] [blame] | 27 | if (!cdc->handle_request) { |
| 28 | return IOINST_CC_STATUS_PRESENT; |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 29 | } |
Halil Pasic | 66dc50f | 2017-10-17 16:04:49 +0200 | [diff] [blame] | 30 | return cdc->handle_request(sch); |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Cornelia Huck | 8fadea2 | 2019-05-07 17:47:33 +0200 | [diff] [blame] | 33 | int s390_ccw_halt(SubchDev *sch) |
| 34 | { |
| 35 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data); |
| 36 | |
| 37 | if (!cdc->handle_halt) { |
| 38 | return -ENOSYS; |
| 39 | } |
| 40 | return cdc->handle_halt(sch); |
| 41 | } |
| 42 | |
| 43 | int s390_ccw_clear(SubchDev *sch) |
| 44 | { |
| 45 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data); |
| 46 | |
| 47 | if (!cdc->handle_clear) { |
| 48 | return -ENOSYS; |
| 49 | } |
| 50 | return cdc->handle_clear(sch); |
| 51 | } |
| 52 | |
Farhan Ali | 46ea384 | 2020-05-05 14:57:54 +0200 | [diff] [blame] | 53 | IOInstEnding s390_ccw_store(SubchDev *sch) |
| 54 | { |
| 55 | S390CCWDeviceClass *cdc = NULL; |
| 56 | int ret = IOINST_CC_EXPECTED; |
| 57 | |
| 58 | /* |
| 59 | * This code is called for both virtual and passthrough devices, |
| 60 | * but only applies to to the latter. This ugly check makes that |
| 61 | * distinction for us. |
| 62 | */ |
| 63 | if (object_dynamic_cast(OBJECT(sch->driver_data), TYPE_S390_CCW)) { |
| 64 | cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data); |
| 65 | } |
| 66 | |
| 67 | if (cdc && cdc->handle_store) { |
| 68 | ret = cdc->handle_store(sch); |
| 69 | } |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 74 | static void s390_ccw_get_dev_info(S390CCWDevice *cdev, |
| 75 | char *sysfsdev, |
| 76 | Error **errp) |
| 77 | { |
| 78 | unsigned int cssid, ssid, devid; |
| 79 | char dev_path[PATH_MAX] = {0}, *tmp; |
| 80 | |
| 81 | if (!sysfsdev) { |
| 82 | error_setg(errp, "No host device provided"); |
| 83 | error_append_hint(errp, |
| 84 | "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if (!realpath(sysfsdev, dev_path)) { |
| 89 | error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev); |
| 90 | return; |
| 91 | } |
| 92 | |
Julia Suvorova | 3e015d8 | 2018-03-01 10:08:06 +0300 | [diff] [blame] | 93 | cdev->mdevid = g_path_get_basename(dev_path); |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 94 | |
| 95 | tmp = basename(dirname(dev_path)); |
| 96 | if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) { |
| 97 | error_setg_errno(errp, errno, "Failed to read %s", tmp); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | cdev->hostid.cssid = cssid; |
| 102 | cdev->hostid.ssid = ssid; |
| 103 | cdev->hostid.devid = devid; |
| 104 | cdev->hostid.valid = true; |
| 105 | } |
| 106 | |
| 107 | static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp) |
| 108 | { |
| 109 | CcwDevice *ccw_dev = CCW_DEVICE(cdev); |
| 110 | CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); |
| 111 | DeviceState *parent = DEVICE(ccw_dev); |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 112 | SubchDev *sch; |
| 113 | int ret; |
| 114 | Error *err = NULL; |
| 115 | |
| 116 | s390_ccw_get_dev_info(cdev, sysfsdev, &err); |
| 117 | if (err) { |
| 118 | goto out_err_propagate; |
| 119 | } |
| 120 | |
Cornelia Huck | 36699ab | 2018-07-23 18:32:21 +0200 | [diff] [blame] | 121 | sch = css_create_sch(ccw_dev->devno, &err); |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 122 | if (!sch) { |
| 123 | goto out_mdevid_free; |
| 124 | } |
| 125 | sch->driver_data = cdev; |
Xiao Feng Ren | bab482d | 2017-05-17 02:48:11 +0200 | [diff] [blame] | 126 | sch->do_subchannel_work = do_subchannel_work_passthrough; |
Eric Farman | c626710 | 2021-06-18 01:25:37 +0200 | [diff] [blame] | 127 | sch->irb_cb = build_irb_passthrough; |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 128 | |
| 129 | ccw_dev->sch = sch; |
| 130 | ret = css_sch_build_schib(sch, &cdev->hostid); |
| 131 | if (ret) { |
| 132 | error_setg_errno(&err, -ret, "%s: Failed to build initial schib", |
| 133 | __func__); |
| 134 | goto out_err; |
| 135 | } |
| 136 | |
| 137 | ck->realize(ccw_dev, &err); |
| 138 | if (err) { |
| 139 | goto out_err; |
| 140 | } |
| 141 | |
| 142 | css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, |
| 143 | parent->hotplugged, 1); |
| 144 | return; |
| 145 | |
| 146 | out_err: |
| 147 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 148 | ccw_dev->sch = NULL; |
| 149 | g_free(sch); |
| 150 | out_mdevid_free: |
| 151 | g_free(cdev->mdevid); |
| 152 | out_err_propagate: |
| 153 | error_propagate(errp, err); |
| 154 | } |
| 155 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 156 | static void s390_ccw_unrealize(S390CCWDevice *cdev) |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 157 | { |
| 158 | CcwDevice *ccw_dev = CCW_DEVICE(cdev); |
| 159 | SubchDev *sch = ccw_dev->sch; |
| 160 | |
| 161 | if (sch) { |
| 162 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 163 | g_free(sch); |
| 164 | ccw_dev->sch = NULL; |
| 165 | } |
| 166 | |
| 167 | g_free(cdev->mdevid); |
| 168 | } |
| 169 | |
Jason J. Herne | 44445d8 | 2019-04-04 10:34:20 -0400 | [diff] [blame] | 170 | static void s390_ccw_instance_init(Object *obj) |
| 171 | { |
| 172 | S390CCWDevice *dev = S390_CCW_DEVICE(obj); |
| 173 | |
| 174 | device_add_bootindex_property(obj, &dev->bootindex, "bootindex", |
Markus Armbruster | 40c2281 | 2020-05-05 17:29:23 +0200 | [diff] [blame] | 175 | "/disk@0,0", DEVICE(obj)); |
Jason J. Herne | 44445d8 | 2019-04-04 10:34:20 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 178 | static void s390_ccw_class_init(ObjectClass *klass, void *data) |
| 179 | { |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 180 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); |
| 181 | |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 182 | cdc->realize = s390_ccw_realize; |
| 183 | cdc->unrealize = s390_ccw_unrealize; |
| 184 | } |
| 185 | |
| 186 | static const TypeInfo s390_ccw_info = { |
| 187 | .name = TYPE_S390_CCW, |
| 188 | .parent = TYPE_CCW_DEVICE, |
Jason J. Herne | 44445d8 | 2019-04-04 10:34:20 -0400 | [diff] [blame] | 189 | .instance_init = s390_ccw_instance_init, |
Dong Jia Shi | a8eac94 | 2017-05-17 02:48:06 +0200 | [diff] [blame] | 190 | .instance_size = sizeof(S390CCWDevice), |
| 191 | .class_size = sizeof(S390CCWDeviceClass), |
| 192 | .class_init = s390_ccw_class_init, |
| 193 | .abstract = true, |
| 194 | }; |
| 195 | |
| 196 | static void register_s390_ccw_type(void) |
| 197 | { |
| 198 | type_register_static(&s390_ccw_info); |
| 199 | } |
| 200 | |
| 201 | type_init(register_s390_ccw_type) |