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 */ 13 14 #include "qemu/osdep.h" 15 #include <libgen.h> 16 #include "qapi/error.h" 17 #include "hw/sysbus.h" 18 #include "hw/s390x/css.h" 19 #include "hw/s390x/css-bridge.h" 20 #include "hw/s390x/s390-ccw.h" 21 22 IOInstEnding s390_ccw_cmd_request(SubchDev *sch) 23 { 24 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data); 25 26 if (!cdc->handle_request) { 27 return IOINST_CC_STATUS_PRESENT; 28 } 29 return cdc->handle_request(sch); 30 } 31 32 static void s390_ccw_get_dev_info(S390CCWDevice *cdev, 33 char *sysfsdev, 34 Error **errp) 35 { 36 unsigned int cssid, ssid, devid; 37 char dev_path[PATH_MAX] = {0}, *tmp; 38 39 if (!sysfsdev) { 40 error_setg(errp, "No host device provided"); 41 error_append_hint(errp, 42 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n"); 43 return; 44 } 45 46 if (!realpath(sysfsdev, dev_path)) { 47 error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev); 48 return; 49 } 50 51 cdev->mdevid = g_path_get_basename(dev_path); 52 53 tmp = basename(dirname(dev_path)); 54 if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) { 55 error_setg_errno(errp, errno, "Failed to read %s", tmp); 56 return; 57 } 58 59 cdev->hostid.cssid = cssid; 60 cdev->hostid.ssid = ssid; 61 cdev->hostid.devid = devid; 62 cdev->hostid.valid = true; 63 } 64 65 static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp) 66 { 67 CcwDevice *ccw_dev = CCW_DEVICE(cdev); 68 CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); 69 DeviceState *parent = DEVICE(ccw_dev); 70 SubchDev *sch; 71 int ret; 72 Error *err = NULL; 73 74 s390_ccw_get_dev_info(cdev, sysfsdev, &err); 75 if (err) { 76 goto out_err_propagate; 77 } 78 79 sch = css_create_sch(ccw_dev->devno, &err); 80 if (!sch) { 81 goto out_mdevid_free; 82 } 83 sch->driver_data = cdev; 84 sch->do_subchannel_work = do_subchannel_work_passthrough; 85 86 ccw_dev->sch = sch; 87 ret = css_sch_build_schib(sch, &cdev->hostid); 88 if (ret) { 89 error_setg_errno(&err, -ret, "%s: Failed to build initial schib", 90 __func__); 91 goto out_err; 92 } 93 94 ck->realize(ccw_dev, &err); 95 if (err) { 96 goto out_err; 97 } 98 99 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 100 parent->hotplugged, 1); 101 return; 102 103 out_err: 104 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 105 ccw_dev->sch = NULL; 106 g_free(sch); 107 out_mdevid_free: 108 g_free(cdev->mdevid); 109 out_err_propagate: 110 error_propagate(errp, err); 111 } 112 113 static void s390_ccw_unrealize(S390CCWDevice *cdev, Error **errp) 114 { 115 CcwDevice *ccw_dev = CCW_DEVICE(cdev); 116 SubchDev *sch = ccw_dev->sch; 117 118 if (sch) { 119 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 120 g_free(sch); 121 ccw_dev->sch = NULL; 122 } 123 124 g_free(cdev->mdevid); 125 } 126 127 static void s390_ccw_class_init(ObjectClass *klass, void *data) 128 { 129 DeviceClass *dc = DEVICE_CLASS(klass); 130 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); 131 132 dc->bus_type = TYPE_VIRTUAL_CSS_BUS; 133 cdc->realize = s390_ccw_realize; 134 cdc->unrealize = s390_ccw_unrealize; 135 } 136 137 static const TypeInfo s390_ccw_info = { 138 .name = TYPE_S390_CCW, 139 .parent = TYPE_CCW_DEVICE, 140 .instance_size = sizeof(S390CCWDevice), 141 .class_size = sizeof(S390CCWDeviceClass), 142 .class_init = s390_ccw_class_init, 143 .abstract = true, 144 }; 145 146 static void register_s390_ccw_type(void) 147 { 148 type_register_static(&s390_ccw_info); 149 } 150 151 type_init(register_s390_ccw_type) 152