Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ccw based virtio transport |
| 3 | * |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 4 | * Copyright IBM Corp. 2012, 2014 |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License (version 2 only) |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel_stat.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/bootmem.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/virtio.h> |
| 18 | #include <linux/virtio_config.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/virtio_ring.h> |
| 22 | #include <linux/pfn.h> |
| 23 | #include <linux/async.h> |
| 24 | #include <linux/wait.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/bitops.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/io.h> |
| 29 | #include <linux/kvm_para.h> |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 30 | #include <linux/notifier.h> |
Martin Schwidefsky | 1ec2772 | 2015-08-20 17:28:44 +0200 | [diff] [blame] | 31 | #include <asm/diag.h> |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 32 | #include <asm/setup.h> |
| 33 | #include <asm/irq.h> |
| 34 | #include <asm/cio.h> |
| 35 | #include <asm/ccwdev.h> |
Cornelia Huck | 6a773cb | 2013-02-28 12:33:17 +0100 | [diff] [blame] | 36 | #include <asm/virtio-ccw.h> |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 37 | #include <asm/isc.h> |
| 38 | #include <asm/airq.h> |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * virtio related functions |
| 42 | */ |
| 43 | |
| 44 | struct vq_config_block { |
| 45 | __u16 index; |
| 46 | __u16 num; |
| 47 | } __packed; |
| 48 | |
| 49 | #define VIRTIO_CCW_CONFIG_SIZE 0x100 |
| 50 | /* same as PCI config space size, should be enough for all drivers */ |
| 51 | |
| 52 | struct virtio_ccw_device { |
| 53 | struct virtio_device vdev; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 54 | __u8 *status; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 55 | __u8 config[VIRTIO_CCW_CONFIG_SIZE]; |
| 56 | struct ccw_device *cdev; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 57 | __u32 curr_io; |
| 58 | int err; |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 59 | unsigned int revision; /* Transport revision */ |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 60 | wait_queue_head_t wait_q; |
| 61 | spinlock_t lock; |
| 62 | struct list_head virtqueues; |
| 63 | unsigned long indicators; |
| 64 | unsigned long indicators2; |
| 65 | struct vq_config_block *config_block; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 66 | bool is_thinint; |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 67 | bool going_away; |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 68 | bool device_lost; |
Cornelia Huck | 431dae7 | 2015-06-29 16:44:01 +0200 | [diff] [blame] | 69 | unsigned int config_ready; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 70 | void *airq_info; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 73 | struct vq_info_block_legacy { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 74 | __u64 queue; |
| 75 | __u32 align; |
| 76 | __u16 index; |
| 77 | __u16 num; |
| 78 | } __packed; |
| 79 | |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 80 | struct vq_info_block { |
| 81 | __u64 desc; |
| 82 | __u32 res0; |
| 83 | __u16 index; |
| 84 | __u16 num; |
| 85 | __u64 avail; |
| 86 | __u64 used; |
| 87 | } __packed; |
| 88 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 89 | struct virtio_feature_desc { |
| 90 | __u32 features; |
| 91 | __u8 index; |
| 92 | } __packed; |
| 93 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 94 | struct virtio_thinint_area { |
| 95 | unsigned long summary_indicator; |
| 96 | unsigned long indicator; |
| 97 | u64 bit_nr; |
| 98 | u8 isc; |
| 99 | } __packed; |
| 100 | |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 101 | struct virtio_rev_info { |
| 102 | __u16 revision; |
| 103 | __u16 length; |
| 104 | __u8 data[]; |
| 105 | }; |
| 106 | |
| 107 | /* the highest virtio-ccw revision we support */ |
Cornelia Huck | af9ca13 | 2014-10-07 16:39:52 +0200 | [diff] [blame] | 108 | #define VIRTIO_CCW_REV_MAX 1 |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 109 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 110 | struct virtio_ccw_vq_info { |
| 111 | struct virtqueue *vq; |
| 112 | int num; |
| 113 | void *queue; |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 114 | union { |
| 115 | struct vq_info_block s; |
| 116 | struct vq_info_block_legacy l; |
| 117 | } *info_block; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 118 | int bit_nr; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 119 | struct list_head node; |
Michael S. Tsirkin | 07e1693 | 2013-02-28 12:33:16 +0100 | [diff] [blame] | 120 | long cookie; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 123 | #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */ |
| 124 | |
| 125 | #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8) |
| 126 | #define MAX_AIRQ_AREAS 20 |
| 127 | |
| 128 | static int virtio_ccw_use_airq = 1; |
| 129 | |
| 130 | struct airq_info { |
| 131 | rwlock_t lock; |
| 132 | u8 summary_indicator; |
| 133 | struct airq_struct airq; |
| 134 | struct airq_iv *aiv; |
| 135 | }; |
| 136 | static struct airq_info *airq_areas[MAX_AIRQ_AREAS]; |
| 137 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 138 | #define CCW_CMD_SET_VQ 0x13 |
| 139 | #define CCW_CMD_VDEV_RESET 0x33 |
| 140 | #define CCW_CMD_SET_IND 0x43 |
| 141 | #define CCW_CMD_SET_CONF_IND 0x53 |
| 142 | #define CCW_CMD_READ_FEAT 0x12 |
| 143 | #define CCW_CMD_WRITE_FEAT 0x11 |
| 144 | #define CCW_CMD_READ_CONF 0x22 |
| 145 | #define CCW_CMD_WRITE_CONF 0x21 |
| 146 | #define CCW_CMD_WRITE_STATUS 0x31 |
| 147 | #define CCW_CMD_READ_VQ_CONF 0x32 |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 148 | #define CCW_CMD_SET_IND_ADAPTER 0x73 |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 149 | #define CCW_CMD_SET_VIRTIO_REV 0x83 |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 150 | |
| 151 | #define VIRTIO_CCW_DOING_SET_VQ 0x00010000 |
| 152 | #define VIRTIO_CCW_DOING_RESET 0x00040000 |
| 153 | #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000 |
| 154 | #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000 |
| 155 | #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000 |
| 156 | #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000 |
| 157 | #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000 |
| 158 | #define VIRTIO_CCW_DOING_SET_IND 0x01000000 |
| 159 | #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000 |
| 160 | #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000 |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 161 | #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000 |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 162 | #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000 |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 163 | #define VIRTIO_CCW_INTPARM_MASK 0xffff0000 |
| 164 | |
| 165 | static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev) |
| 166 | { |
| 167 | return container_of(vdev, struct virtio_ccw_device, vdev); |
| 168 | } |
| 169 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 170 | static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info) |
| 171 | { |
| 172 | unsigned long i, flags; |
| 173 | |
| 174 | write_lock_irqsave(&info->lock, flags); |
| 175 | for (i = 0; i < airq_iv_end(info->aiv); i++) { |
| 176 | if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) { |
| 177 | airq_iv_free_bit(info->aiv, i); |
| 178 | airq_iv_set_ptr(info->aiv, i, 0); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | write_unlock_irqrestore(&info->lock, flags); |
| 183 | } |
| 184 | |
| 185 | static void virtio_airq_handler(struct airq_struct *airq) |
| 186 | { |
| 187 | struct airq_info *info = container_of(airq, struct airq_info, airq); |
| 188 | unsigned long ai; |
| 189 | |
| 190 | inc_irq_stat(IRQIO_VAI); |
| 191 | read_lock(&info->lock); |
| 192 | /* Walk through indicators field, summary indicator active. */ |
| 193 | for (ai = 0;;) { |
| 194 | ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv)); |
| 195 | if (ai == -1UL) |
| 196 | break; |
| 197 | vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai)); |
| 198 | } |
| 199 | info->summary_indicator = 0; |
| 200 | smp_wmb(); |
| 201 | /* Walk through indicators field, summary indicator not active. */ |
| 202 | for (ai = 0;;) { |
| 203 | ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv)); |
| 204 | if (ai == -1UL) |
| 205 | break; |
| 206 | vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai)); |
| 207 | } |
| 208 | read_unlock(&info->lock); |
| 209 | } |
| 210 | |
| 211 | static struct airq_info *new_airq_info(void) |
| 212 | { |
| 213 | struct airq_info *info; |
| 214 | int rc; |
| 215 | |
| 216 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 217 | if (!info) |
| 218 | return NULL; |
| 219 | rwlock_init(&info->lock); |
| 220 | info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR); |
| 221 | if (!info->aiv) { |
| 222 | kfree(info); |
| 223 | return NULL; |
| 224 | } |
| 225 | info->airq.handler = virtio_airq_handler; |
| 226 | info->airq.lsi_ptr = &info->summary_indicator; |
| 227 | info->airq.lsi_mask = 0xff; |
| 228 | info->airq.isc = VIRTIO_AIRQ_ISC; |
| 229 | rc = register_adapter_interrupt(&info->airq); |
| 230 | if (rc) { |
| 231 | airq_iv_release(info->aiv); |
| 232 | kfree(info); |
| 233 | return NULL; |
| 234 | } |
| 235 | return info; |
| 236 | } |
| 237 | |
| 238 | static void destroy_airq_info(struct airq_info *info) |
| 239 | { |
| 240 | if (!info) |
| 241 | return; |
| 242 | |
| 243 | unregister_adapter_interrupt(&info->airq); |
| 244 | airq_iv_release(info->aiv); |
| 245 | kfree(info); |
| 246 | } |
| 247 | |
| 248 | static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs, |
| 249 | u64 *first, void **airq_info) |
| 250 | { |
| 251 | int i, j; |
| 252 | struct airq_info *info; |
| 253 | unsigned long indicator_addr = 0; |
| 254 | unsigned long bit, flags; |
| 255 | |
| 256 | for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) { |
| 257 | if (!airq_areas[i]) |
| 258 | airq_areas[i] = new_airq_info(); |
| 259 | info = airq_areas[i]; |
| 260 | if (!info) |
| 261 | return 0; |
| 262 | write_lock_irqsave(&info->lock, flags); |
| 263 | bit = airq_iv_alloc(info->aiv, nvqs); |
| 264 | if (bit == -1UL) { |
| 265 | /* Not enough vacancies. */ |
| 266 | write_unlock_irqrestore(&info->lock, flags); |
| 267 | continue; |
| 268 | } |
| 269 | *first = bit; |
| 270 | *airq_info = info; |
| 271 | indicator_addr = (unsigned long)info->aiv->vector; |
| 272 | for (j = 0; j < nvqs; j++) { |
| 273 | airq_iv_set_ptr(info->aiv, bit + j, |
| 274 | (unsigned long)vqs[j]); |
| 275 | } |
| 276 | write_unlock_irqrestore(&info->lock, flags); |
| 277 | } |
| 278 | return indicator_addr; |
| 279 | } |
| 280 | |
| 281 | static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev) |
| 282 | { |
| 283 | struct virtio_ccw_vq_info *info; |
| 284 | |
| 285 | list_for_each_entry(info, &vcdev->virtqueues, node) |
| 286 | drop_airq_indicator(info->vq, vcdev->airq_info); |
| 287 | } |
| 288 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 289 | static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag) |
| 290 | { |
| 291 | unsigned long flags; |
| 292 | __u32 ret; |
| 293 | |
| 294 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); |
| 295 | if (vcdev->err) |
| 296 | ret = 0; |
| 297 | else |
| 298 | ret = vcdev->curr_io & flag; |
| 299 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); |
| 300 | return ret; |
| 301 | } |
| 302 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 303 | static int ccw_io_helper(struct virtio_ccw_device *vcdev, |
| 304 | struct ccw1 *ccw, __u32 intparm) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 305 | { |
| 306 | int ret; |
| 307 | unsigned long flags; |
| 308 | int flag = intparm & VIRTIO_CCW_INTPARM_MASK; |
| 309 | |
Christian Borntraeger | b26ba22 | 2013-01-07 15:51:52 +0100 | [diff] [blame] | 310 | do { |
| 311 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); |
| 312 | ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0); |
Cornelia Huck | 99437a2 | 2013-04-04 10:25:06 +0200 | [diff] [blame] | 313 | if (!ret) { |
| 314 | if (!vcdev->curr_io) |
| 315 | vcdev->err = 0; |
Christian Borntraeger | b26ba22 | 2013-01-07 15:51:52 +0100 | [diff] [blame] | 316 | vcdev->curr_io |= flag; |
Cornelia Huck | 99437a2 | 2013-04-04 10:25:06 +0200 | [diff] [blame] | 317 | } |
Christian Borntraeger | b26ba22 | 2013-01-07 15:51:52 +0100 | [diff] [blame] | 318 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); |
| 319 | cpu_relax(); |
| 320 | } while (ret == -EBUSY); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 321 | wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0); |
| 322 | return ret ? ret : vcdev->err; |
| 323 | } |
| 324 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 325 | static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev, |
| 326 | struct ccw1 *ccw) |
| 327 | { |
| 328 | int ret; |
| 329 | unsigned long *indicatorp = NULL; |
| 330 | struct virtio_thinint_area *thinint_area = NULL; |
| 331 | struct airq_info *airq_info = vcdev->airq_info; |
| 332 | |
| 333 | if (vcdev->is_thinint) { |
| 334 | thinint_area = kzalloc(sizeof(*thinint_area), |
| 335 | GFP_DMA | GFP_KERNEL); |
| 336 | if (!thinint_area) |
| 337 | return; |
| 338 | thinint_area->summary_indicator = |
| 339 | (unsigned long) &airq_info->summary_indicator; |
| 340 | thinint_area->isc = VIRTIO_AIRQ_ISC; |
| 341 | ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER; |
| 342 | ccw->count = sizeof(*thinint_area); |
| 343 | ccw->cda = (__u32)(unsigned long) thinint_area; |
| 344 | } else { |
Cornelia Huck | c235082 | 2016-03-01 13:44:53 +0100 | [diff] [blame] | 345 | /* payload is the address of the indicators */ |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 346 | indicatorp = kmalloc(sizeof(&vcdev->indicators), |
| 347 | GFP_DMA | GFP_KERNEL); |
| 348 | if (!indicatorp) |
| 349 | return; |
| 350 | *indicatorp = 0; |
| 351 | ccw->cmd_code = CCW_CMD_SET_IND; |
Cornelia Huck | c235082 | 2016-03-01 13:44:53 +0100 | [diff] [blame] | 352 | ccw->count = sizeof(&vcdev->indicators); |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 353 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 354 | } |
| 355 | /* Deregister indicators from host. */ |
| 356 | vcdev->indicators = 0; |
| 357 | ccw->flags = 0; |
| 358 | ret = ccw_io_helper(vcdev, ccw, |
| 359 | vcdev->is_thinint ? |
| 360 | VIRTIO_CCW_DOING_SET_IND_ADAPTER : |
| 361 | VIRTIO_CCW_DOING_SET_IND); |
| 362 | if (ret && (ret != -ENODEV)) |
| 363 | dev_info(&vcdev->cdev->dev, |
| 364 | "Failed to deregister indicators (%d)\n", ret); |
| 365 | else if (vcdev->is_thinint) |
| 366 | virtio_ccw_drop_indicators(vcdev); |
| 367 | kfree(indicatorp); |
| 368 | kfree(thinint_area); |
| 369 | } |
| 370 | |
Martin Schwidefsky | 1ec2772 | 2015-08-20 17:28:44 +0200 | [diff] [blame] | 371 | static inline long __do_kvm_notify(struct subchannel_id schid, |
| 372 | unsigned long queue_index, |
| 373 | long cookie) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 374 | { |
| 375 | register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY; |
| 376 | register struct subchannel_id __schid asm("2") = schid; |
| 377 | register unsigned long __index asm("3") = queue_index; |
| 378 | register long __rc asm("2"); |
Michael S. Tsirkin | 07e1693 | 2013-02-28 12:33:16 +0100 | [diff] [blame] | 379 | register long __cookie asm("4") = cookie; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 380 | |
| 381 | asm volatile ("diag 2,4,0x500\n" |
Michael S. Tsirkin | 07e1693 | 2013-02-28 12:33:16 +0100 | [diff] [blame] | 382 | : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index), |
| 383 | "d"(__cookie) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 384 | : "memory", "cc"); |
| 385 | return __rc; |
| 386 | } |
| 387 | |
Martin Schwidefsky | 1ec2772 | 2015-08-20 17:28:44 +0200 | [diff] [blame] | 388 | static inline long do_kvm_notify(struct subchannel_id schid, |
| 389 | unsigned long queue_index, |
| 390 | long cookie) |
| 391 | { |
| 392 | diag_stat_inc(DIAG_STAT_X500); |
| 393 | return __do_kvm_notify(schid, queue_index, cookie); |
| 394 | } |
| 395 | |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 396 | static bool virtio_ccw_kvm_notify(struct virtqueue *vq) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 397 | { |
| 398 | struct virtio_ccw_vq_info *info = vq->priv; |
| 399 | struct virtio_ccw_device *vcdev; |
| 400 | struct subchannel_id schid; |
| 401 | |
| 402 | vcdev = to_vc_device(info->vq->vdev); |
| 403 | ccw_device_get_schid(vcdev->cdev, &schid); |
Linus Torvalds | 01227a8 | 2013-05-05 14:47:31 -0700 | [diff] [blame] | 404 | info->cookie = do_kvm_notify(schid, vq->index, info->cookie); |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 405 | if (info->cookie < 0) |
| 406 | return false; |
| 407 | return true; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 410 | static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev, |
| 411 | struct ccw1 *ccw, int index) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 412 | { |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 413 | int ret; |
| 414 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 415 | vcdev->config_block->index = index; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 416 | ccw->cmd_code = CCW_CMD_READ_VQ_CONF; |
| 417 | ccw->flags = 0; |
| 418 | ccw->count = sizeof(struct vq_config_block); |
| 419 | ccw->cda = (__u32)(unsigned long)(vcdev->config_block); |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 420 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF); |
| 421 | if (ret) |
| 422 | return ret; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 423 | return vcdev->config_block->num; |
| 424 | } |
| 425 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 426 | static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 427 | { |
| 428 | struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev); |
| 429 | struct virtio_ccw_vq_info *info = vq->priv; |
| 430 | unsigned long flags; |
| 431 | unsigned long size; |
| 432 | int ret; |
Rusty Russell | 9d0ca6e | 2013-03-21 14:17:34 +0000 | [diff] [blame] | 433 | unsigned int index = vq->index; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 434 | |
| 435 | /* Remove from our list. */ |
| 436 | spin_lock_irqsave(&vcdev->lock, flags); |
| 437 | list_del(&info->node); |
| 438 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 439 | |
| 440 | /* Release from host. */ |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 441 | if (vcdev->revision == 0) { |
| 442 | info->info_block->l.queue = 0; |
| 443 | info->info_block->l.align = 0; |
| 444 | info->info_block->l.index = index; |
| 445 | info->info_block->l.num = 0; |
| 446 | ccw->count = sizeof(info->info_block->l); |
| 447 | } else { |
| 448 | info->info_block->s.desc = 0; |
| 449 | info->info_block->s.index = index; |
| 450 | info->info_block->s.num = 0; |
| 451 | info->info_block->s.avail = 0; |
| 452 | info->info_block->s.used = 0; |
| 453 | ccw->count = sizeof(info->info_block->s); |
| 454 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 455 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 456 | ccw->flags = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 457 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 458 | ret = ccw_io_helper(vcdev, ccw, |
| 459 | VIRTIO_CCW_DOING_SET_VQ | index); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 460 | /* |
| 461 | * -ENODEV isn't considered an error: The device is gone anyway. |
| 462 | * This may happen on device detach. |
| 463 | */ |
| 464 | if (ret && (ret != -ENODEV)) |
| 465 | dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d", |
| 466 | ret, index); |
| 467 | |
| 468 | vring_del_virtqueue(vq); |
| 469 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 470 | free_pages_exact(info->queue, size); |
| 471 | kfree(info->info_block); |
| 472 | kfree(info); |
| 473 | } |
| 474 | |
| 475 | static void virtio_ccw_del_vqs(struct virtio_device *vdev) |
| 476 | { |
| 477 | struct virtqueue *vq, *n; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 478 | struct ccw1 *ccw; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 479 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 480 | |
| 481 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 482 | if (!ccw) |
| 483 | return; |
| 484 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 485 | virtio_ccw_drop_indicator(vcdev, ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 486 | |
| 487 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 488 | virtio_ccw_del_vq(vq, ccw); |
| 489 | |
| 490 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev, |
| 494 | int i, vq_callback_t *callback, |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 495 | const char *name, |
| 496 | struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 497 | { |
| 498 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 499 | int err; |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 500 | struct virtqueue *vq = NULL; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 501 | struct virtio_ccw_vq_info *info; |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 502 | unsigned long size = 0; /* silence the compiler */ |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 503 | unsigned long flags; |
| 504 | |
| 505 | /* Allocate queue. */ |
| 506 | info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL); |
| 507 | if (!info) { |
| 508 | dev_warn(&vcdev->cdev->dev, "no info\n"); |
| 509 | err = -ENOMEM; |
| 510 | goto out_err; |
| 511 | } |
| 512 | info->info_block = kzalloc(sizeof(*info->info_block), |
| 513 | GFP_DMA | GFP_KERNEL); |
| 514 | if (!info->info_block) { |
| 515 | dev_warn(&vcdev->cdev->dev, "no info block\n"); |
| 516 | err = -ENOMEM; |
| 517 | goto out_err; |
| 518 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 519 | info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i); |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 520 | if (info->num < 0) { |
| 521 | err = info->num; |
| 522 | goto out_err; |
| 523 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 524 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 525 | info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| 526 | if (info->queue == NULL) { |
| 527 | dev_warn(&vcdev->cdev->dev, "no queue\n"); |
| 528 | err = -ENOMEM; |
| 529 | goto out_err; |
| 530 | } |
| 531 | |
| 532 | vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev, |
| 533 | true, info->queue, virtio_ccw_kvm_notify, |
| 534 | callback, name); |
| 535 | if (!vq) { |
| 536 | /* For now, we fail if we can't get the requested size. */ |
| 537 | dev_warn(&vcdev->cdev->dev, "no vq\n"); |
| 538 | err = -ENOMEM; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 539 | goto out_err; |
| 540 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 541 | |
| 542 | /* Register it with the host. */ |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 543 | if (vcdev->revision == 0) { |
| 544 | info->info_block->l.queue = (__u64)info->queue; |
| 545 | info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN; |
| 546 | info->info_block->l.index = i; |
| 547 | info->info_block->l.num = info->num; |
| 548 | ccw->count = sizeof(info->info_block->l); |
| 549 | } else { |
| 550 | info->info_block->s.desc = (__u64)info->queue; |
| 551 | info->info_block->s.index = i; |
| 552 | info->info_block->s.num = info->num; |
| 553 | info->info_block->s.avail = (__u64)virtqueue_get_avail(vq); |
| 554 | info->info_block->s.used = (__u64)virtqueue_get_used(vq); |
| 555 | ccw->count = sizeof(info->info_block->s); |
| 556 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 557 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 558 | ccw->flags = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 559 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 560 | err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 561 | if (err) { |
| 562 | dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n"); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 563 | goto out_err; |
| 564 | } |
| 565 | |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 566 | info->vq = vq; |
| 567 | vq->priv = info; |
| 568 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 569 | /* Save it to our list. */ |
| 570 | spin_lock_irqsave(&vcdev->lock, flags); |
| 571 | list_add(&info->node, &vcdev->virtqueues); |
| 572 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 573 | |
| 574 | return vq; |
| 575 | |
| 576 | out_err: |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 577 | if (vq) |
| 578 | vring_del_virtqueue(vq); |
| 579 | if (info) { |
| 580 | if (info->queue) |
| 581 | free_pages_exact(info->queue, size); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 582 | kfree(info->info_block); |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 583 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 584 | kfree(info); |
| 585 | return ERR_PTR(err); |
| 586 | } |
| 587 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 588 | static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev, |
| 589 | struct virtqueue *vqs[], int nvqs, |
| 590 | struct ccw1 *ccw) |
| 591 | { |
| 592 | int ret; |
| 593 | struct virtio_thinint_area *thinint_area = NULL; |
| 594 | struct airq_info *info; |
| 595 | |
| 596 | thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL); |
| 597 | if (!thinint_area) { |
| 598 | ret = -ENOMEM; |
| 599 | goto out; |
| 600 | } |
| 601 | /* Try to get an indicator. */ |
| 602 | thinint_area->indicator = get_airq_indicator(vqs, nvqs, |
| 603 | &thinint_area->bit_nr, |
| 604 | &vcdev->airq_info); |
| 605 | if (!thinint_area->indicator) { |
| 606 | ret = -ENOSPC; |
| 607 | goto out; |
| 608 | } |
| 609 | info = vcdev->airq_info; |
| 610 | thinint_area->summary_indicator = |
| 611 | (unsigned long) &info->summary_indicator; |
| 612 | thinint_area->isc = VIRTIO_AIRQ_ISC; |
| 613 | ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER; |
| 614 | ccw->flags = CCW_FLAG_SLI; |
| 615 | ccw->count = sizeof(*thinint_area); |
| 616 | ccw->cda = (__u32)(unsigned long)thinint_area; |
| 617 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER); |
| 618 | if (ret) { |
| 619 | if (ret == -EOPNOTSUPP) { |
| 620 | /* |
| 621 | * The host does not support adapter interrupts |
| 622 | * for virtio-ccw, stop trying. |
| 623 | */ |
| 624 | virtio_ccw_use_airq = 0; |
| 625 | pr_info("Adapter interrupts unsupported on host\n"); |
| 626 | } else |
| 627 | dev_warn(&vcdev->cdev->dev, |
| 628 | "enabling adapter interrupts = %d\n", ret); |
| 629 | virtio_ccw_drop_indicators(vcdev); |
| 630 | } |
| 631 | out: |
| 632 | kfree(thinint_area); |
| 633 | return ret; |
| 634 | } |
| 635 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 636 | static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 637 | struct virtqueue *vqs[], |
| 638 | vq_callback_t *callbacks[], |
Stefan Hajnoczi | f7ad26f | 2015-12-17 16:53:43 +0800 | [diff] [blame] | 639 | const char * const names[]) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 640 | { |
| 641 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 642 | unsigned long *indicatorp = NULL; |
| 643 | int ret, i; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 644 | struct ccw1 *ccw; |
| 645 | |
| 646 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 647 | if (!ccw) |
| 648 | return -ENOMEM; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 649 | |
| 650 | for (i = 0; i < nvqs; ++i) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 651 | vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i], |
| 652 | ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 653 | if (IS_ERR(vqs[i])) { |
| 654 | ret = PTR_ERR(vqs[i]); |
| 655 | vqs[i] = NULL; |
| 656 | goto out; |
| 657 | } |
| 658 | } |
| 659 | ret = -ENOMEM; |
Cornelia Huck | c235082 | 2016-03-01 13:44:53 +0100 | [diff] [blame] | 660 | /* |
| 661 | * We need a data area under 2G to communicate. Our payload is |
| 662 | * the address of the indicators. |
| 663 | */ |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 664 | indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL); |
| 665 | if (!indicatorp) |
| 666 | goto out; |
| 667 | *indicatorp = (unsigned long) &vcdev->indicators; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 668 | if (vcdev->is_thinint) { |
| 669 | ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw); |
| 670 | if (ret) |
| 671 | /* no error, just fall back to legacy interrupts */ |
| 672 | vcdev->is_thinint = 0; |
| 673 | } |
| 674 | if (!vcdev->is_thinint) { |
| 675 | /* Register queue indicators with host. */ |
| 676 | vcdev->indicators = 0; |
| 677 | ccw->cmd_code = CCW_CMD_SET_IND; |
| 678 | ccw->flags = 0; |
Cornelia Huck | c235082 | 2016-03-01 13:44:53 +0100 | [diff] [blame] | 679 | ccw->count = sizeof(&vcdev->indicators); |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 680 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 681 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND); |
| 682 | if (ret) |
| 683 | goto out; |
| 684 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 685 | /* Register indicators2 with host for config changes */ |
| 686 | *indicatorp = (unsigned long) &vcdev->indicators2; |
| 687 | vcdev->indicators2 = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 688 | ccw->cmd_code = CCW_CMD_SET_CONF_IND; |
| 689 | ccw->flags = 0; |
Cornelia Huck | c235082 | 2016-03-01 13:44:53 +0100 | [diff] [blame] | 690 | ccw->count = sizeof(&vcdev->indicators2); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 691 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 692 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 693 | if (ret) |
| 694 | goto out; |
| 695 | |
| 696 | kfree(indicatorp); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 697 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 698 | return 0; |
| 699 | out: |
| 700 | kfree(indicatorp); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 701 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 702 | virtio_ccw_del_vqs(vdev); |
| 703 | return ret; |
| 704 | } |
| 705 | |
| 706 | static void virtio_ccw_reset(struct virtio_device *vdev) |
| 707 | { |
| 708 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 709 | struct ccw1 *ccw; |
| 710 | |
| 711 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 712 | if (!ccw) |
| 713 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 714 | |
| 715 | /* Zero status bits. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 716 | *vcdev->status = 0; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 717 | |
| 718 | /* Send a reset ccw on device. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 719 | ccw->cmd_code = CCW_CMD_VDEV_RESET; |
| 720 | ccw->flags = 0; |
| 721 | ccw->count = 0; |
| 722 | ccw->cda = 0; |
| 723 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET); |
| 724 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 727 | static u64 virtio_ccw_get_features(struct virtio_device *vdev) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 728 | { |
| 729 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 730 | struct virtio_feature_desc *features; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 731 | int ret; |
| 732 | u64 rc; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 733 | struct ccw1 *ccw; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 734 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 735 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 736 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 737 | return 0; |
| 738 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 739 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
| 740 | if (!features) { |
| 741 | rc = 0; |
| 742 | goto out_free; |
| 743 | } |
| 744 | /* Read the feature bits from the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 745 | features->index = 0; |
| 746 | ccw->cmd_code = CCW_CMD_READ_FEAT; |
| 747 | ccw->flags = 0; |
| 748 | ccw->count = sizeof(*features); |
| 749 | ccw->cda = (__u32)(unsigned long)features; |
| 750 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
| 751 | if (ret) { |
| 752 | rc = 0; |
| 753 | goto out_free; |
| 754 | } |
| 755 | |
| 756 | rc = le32_to_cpu(features->features); |
| 757 | |
Michael S. Tsirkin | ce15408 | 2014-12-04 18:59:50 +0200 | [diff] [blame] | 758 | if (vcdev->revision == 0) |
| 759 | goto out_free; |
| 760 | |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 761 | /* Read second half of the feature bits from the host. */ |
| 762 | features->index = 1; |
| 763 | ccw->cmd_code = CCW_CMD_READ_FEAT; |
| 764 | ccw->flags = 0; |
| 765 | ccw->count = sizeof(*features); |
| 766 | ccw->cda = (__u32)(unsigned long)features; |
| 767 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
| 768 | if (ret == 0) |
| 769 | rc |= (u64)le32_to_cpu(features->features) << 32; |
| 770 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 771 | out_free: |
| 772 | kfree(features); |
| 773 | kfree(ccw); |
| 774 | return rc; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 777 | static int virtio_ccw_finalize_features(struct virtio_device *vdev) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 778 | { |
| 779 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 780 | struct virtio_feature_desc *features; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 781 | struct ccw1 *ccw; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 782 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 783 | |
Michael S. Tsirkin | f13d8bc | 2014-12-09 14:46:44 +0200 | [diff] [blame] | 784 | if (vcdev->revision >= 1 && |
Michael S. Tsirkin | 4d23676 | 2014-12-04 19:16:43 +0200 | [diff] [blame] | 785 | !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
| 786 | dev_err(&vdev->dev, "virtio: device uses revision 1 " |
| 787 | "but does not have VIRTIO_F_VERSION_1\n"); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 791 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 792 | if (!ccw) |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 793 | return -ENOMEM; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 794 | |
| 795 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 796 | if (!features) { |
| 797 | ret = -ENOMEM; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 798 | goto out_free; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 799 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 800 | /* Give virtio_ring a chance to accept features. */ |
| 801 | vring_transport_features(vdev); |
| 802 | |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 803 | features->index = 0; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 804 | features->features = cpu_to_le32((u32)vdev->features); |
| 805 | /* Write the first half of the feature bits to the host. */ |
| 806 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
| 807 | ccw->flags = 0; |
| 808 | ccw->count = sizeof(*features); |
| 809 | ccw->cda = (__u32)(unsigned long)features; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 810 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
| 811 | if (ret) |
| 812 | goto out_free; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 813 | |
Michael S. Tsirkin | ce15408 | 2014-12-04 18:59:50 +0200 | [diff] [blame] | 814 | if (vcdev->revision == 0) |
| 815 | goto out_free; |
| 816 | |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 817 | features->index = 1; |
| 818 | features->features = cpu_to_le32(vdev->features >> 32); |
| 819 | /* Write the second half of the feature bits to the host. */ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 820 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
| 821 | ccw->flags = 0; |
| 822 | ccw->count = sizeof(*features); |
| 823 | ccw->cda = (__u32)(unsigned long)features; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 824 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 825 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 826 | out_free: |
| 827 | kfree(features); |
| 828 | kfree(ccw); |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 829 | |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 830 | return ret; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void virtio_ccw_get_config(struct virtio_device *vdev, |
| 834 | unsigned int offset, void *buf, unsigned len) |
| 835 | { |
| 836 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 837 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 838 | struct ccw1 *ccw; |
| 839 | void *config_area; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 840 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 841 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 842 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 843 | return; |
| 844 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 845 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 846 | if (!config_area) |
| 847 | goto out_free; |
| 848 | |
| 849 | /* Read the config area from the host. */ |
| 850 | ccw->cmd_code = CCW_CMD_READ_CONF; |
| 851 | ccw->flags = 0; |
| 852 | ccw->count = offset + len; |
| 853 | ccw->cda = (__u32)(unsigned long)config_area; |
| 854 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG); |
| 855 | if (ret) |
| 856 | goto out_free; |
| 857 | |
Cornelia Huck | 431dae7 | 2015-06-29 16:44:01 +0200 | [diff] [blame] | 858 | memcpy(vcdev->config, config_area, offset + len); |
| 859 | if (buf) |
| 860 | memcpy(buf, &vcdev->config[offset], len); |
| 861 | if (vcdev->config_ready < offset + len) |
| 862 | vcdev->config_ready = offset + len; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 863 | |
| 864 | out_free: |
| 865 | kfree(config_area); |
| 866 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static void virtio_ccw_set_config(struct virtio_device *vdev, |
| 870 | unsigned int offset, const void *buf, |
| 871 | unsigned len) |
| 872 | { |
| 873 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 874 | struct ccw1 *ccw; |
| 875 | void *config_area; |
| 876 | |
| 877 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 878 | if (!ccw) |
| 879 | return; |
| 880 | |
| 881 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 882 | if (!config_area) |
| 883 | goto out_free; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 884 | |
Cornelia Huck | 431dae7 | 2015-06-29 16:44:01 +0200 | [diff] [blame] | 885 | /* Make sure we don't overwrite fields. */ |
| 886 | if (vcdev->config_ready < offset) |
| 887 | virtio_ccw_get_config(vdev, 0, NULL, offset); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 888 | memcpy(&vcdev->config[offset], buf, len); |
| 889 | /* Write the config area to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 890 | memcpy(config_area, vcdev->config, sizeof(vcdev->config)); |
| 891 | ccw->cmd_code = CCW_CMD_WRITE_CONF; |
| 892 | ccw->flags = 0; |
| 893 | ccw->count = offset + len; |
| 894 | ccw->cda = (__u32)(unsigned long)config_area; |
| 895 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG); |
| 896 | |
| 897 | out_free: |
| 898 | kfree(config_area); |
| 899 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static u8 virtio_ccw_get_status(struct virtio_device *vdev) |
| 903 | { |
| 904 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 905 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 906 | return *vcdev->status; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status) |
| 910 | { |
| 911 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 912 | u8 old_status = *vcdev->status; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 913 | struct ccw1 *ccw; |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 914 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 915 | |
| 916 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 917 | if (!ccw) |
| 918 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 919 | |
| 920 | /* Write the status to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 921 | *vcdev->status = status; |
| 922 | ccw->cmd_code = CCW_CMD_WRITE_STATUS; |
| 923 | ccw->flags = 0; |
| 924 | ccw->count = sizeof(status); |
| 925 | ccw->cda = (__u32)(unsigned long)vcdev->status; |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 926 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS); |
| 927 | /* Write failed? We assume status is unchanged. */ |
| 928 | if (ret) |
| 929 | *vcdev->status = old_status; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 930 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static struct virtio_config_ops virtio_ccw_config_ops = { |
| 934 | .get_features = virtio_ccw_get_features, |
| 935 | .finalize_features = virtio_ccw_finalize_features, |
| 936 | .get = virtio_ccw_get_config, |
| 937 | .set = virtio_ccw_set_config, |
| 938 | .get_status = virtio_ccw_get_status, |
| 939 | .set_status = virtio_ccw_set_status, |
| 940 | .reset = virtio_ccw_reset, |
| 941 | .find_vqs = virtio_ccw_find_vqs, |
| 942 | .del_vqs = virtio_ccw_del_vqs, |
| 943 | }; |
| 944 | |
| 945 | |
| 946 | /* |
| 947 | * ccw bus driver related functions |
| 948 | */ |
| 949 | |
| 950 | static void virtio_ccw_release_dev(struct device *_d) |
| 951 | { |
Geliang Tang | 15e90f5 | 2016-03-01 13:44:52 +0100 | [diff] [blame] | 952 | struct virtio_device *dev = dev_to_virtio(_d); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 953 | struct virtio_ccw_device *vcdev = to_vc_device(dev); |
| 954 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 955 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 956 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 957 | kfree(vcdev); |
| 958 | } |
| 959 | |
| 960 | static int irb_is_error(struct irb *irb) |
| 961 | { |
| 962 | if (scsw_cstat(&irb->scsw) != 0) |
| 963 | return 1; |
| 964 | if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) |
| 965 | return 1; |
| 966 | if (scsw_cc(&irb->scsw) != 0) |
| 967 | return 1; |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev, |
| 972 | int index) |
| 973 | { |
| 974 | struct virtio_ccw_vq_info *info; |
| 975 | unsigned long flags; |
| 976 | struct virtqueue *vq; |
| 977 | |
| 978 | vq = NULL; |
| 979 | spin_lock_irqsave(&vcdev->lock, flags); |
| 980 | list_for_each_entry(info, &vcdev->virtqueues, node) { |
Rusty Russell | 9d0ca6e | 2013-03-21 14:17:34 +0000 | [diff] [blame] | 981 | if (info->vq->index == index) { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 982 | vq = info->vq; |
| 983 | break; |
| 984 | } |
| 985 | } |
| 986 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 987 | return vq; |
| 988 | } |
| 989 | |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame] | 990 | static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev, |
| 991 | __u32 activity) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 992 | { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 993 | if (vcdev->curr_io & activity) { |
| 994 | switch (activity) { |
| 995 | case VIRTIO_CCW_DOING_READ_FEAT: |
| 996 | case VIRTIO_CCW_DOING_WRITE_FEAT: |
| 997 | case VIRTIO_CCW_DOING_READ_CONFIG: |
| 998 | case VIRTIO_CCW_DOING_WRITE_CONFIG: |
| 999 | case VIRTIO_CCW_DOING_WRITE_STATUS: |
| 1000 | case VIRTIO_CCW_DOING_SET_VQ: |
| 1001 | case VIRTIO_CCW_DOING_SET_IND: |
| 1002 | case VIRTIO_CCW_DOING_SET_CONF_IND: |
| 1003 | case VIRTIO_CCW_DOING_RESET: |
| 1004 | case VIRTIO_CCW_DOING_READ_VQ_CONF: |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1005 | case VIRTIO_CCW_DOING_SET_IND_ADAPTER: |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1006 | case VIRTIO_CCW_DOING_SET_VIRTIO_REV: |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1007 | vcdev->curr_io &= ~activity; |
| 1008 | wake_up(&vcdev->wait_q); |
| 1009 | break; |
| 1010 | default: |
| 1011 | /* don't know what to do... */ |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame] | 1012 | dev_warn(&vcdev->cdev->dev, |
| 1013 | "Suspicious activity '%08x'\n", activity); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1014 | WARN_ON(1); |
| 1015 | break; |
| 1016 | } |
| 1017 | } |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | static void virtio_ccw_int_handler(struct ccw_device *cdev, |
| 1021 | unsigned long intparm, |
| 1022 | struct irb *irb) |
| 1023 | { |
| 1024 | __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK; |
| 1025 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 1026 | int i; |
| 1027 | struct virtqueue *vq; |
| 1028 | |
| 1029 | if (!vcdev) |
| 1030 | return; |
| 1031 | if (IS_ERR(irb)) { |
| 1032 | vcdev->err = PTR_ERR(irb); |
| 1033 | virtio_ccw_check_activity(vcdev, activity); |
| 1034 | /* Don't poke around indicators, something's wrong. */ |
| 1035 | return; |
| 1036 | } |
| 1037 | /* Check if it's a notification from the host. */ |
| 1038 | if ((intparm == 0) && |
| 1039 | (scsw_stctl(&irb->scsw) == |
| 1040 | (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) { |
| 1041 | /* OK */ |
| 1042 | } |
| 1043 | if (irb_is_error(irb)) { |
| 1044 | /* Command reject? */ |
| 1045 | if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) && |
| 1046 | (irb->ecw[0] & SNS0_CMD_REJECT)) |
| 1047 | vcdev->err = -EOPNOTSUPP; |
| 1048 | else |
| 1049 | /* Map everything else to -EIO. */ |
| 1050 | vcdev->err = -EIO; |
| 1051 | } |
| 1052 | virtio_ccw_check_activity(vcdev, activity); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1053 | for_each_set_bit(i, &vcdev->indicators, |
| 1054 | sizeof(vcdev->indicators) * BITS_PER_BYTE) { |
| 1055 | /* The bit clear must happen before the vring kick. */ |
| 1056 | clear_bit(i, &vcdev->indicators); |
| 1057 | barrier(); |
| 1058 | vq = virtio_ccw_vq_by_ind(vcdev, i); |
| 1059 | vring_interrupt(0, vq); |
| 1060 | } |
| 1061 | if (test_bit(0, &vcdev->indicators2)) { |
Michael S. Tsirkin | 016c98c | 2014-10-14 10:40:34 +1030 | [diff] [blame] | 1062 | virtio_config_changed(&vcdev->vdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1063 | clear_bit(0, &vcdev->indicators2); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * We usually want to autoonline all devices, but give the admin |
| 1069 | * a way to exempt devices from this. |
| 1070 | */ |
| 1071 | #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \ |
| 1072 | (8*sizeof(long))) |
| 1073 | static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS]; |
| 1074 | |
| 1075 | static char *no_auto = ""; |
| 1076 | |
| 1077 | module_param(no_auto, charp, 0444); |
| 1078 | MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined"); |
| 1079 | |
| 1080 | static int virtio_ccw_check_autoonline(struct ccw_device *cdev) |
| 1081 | { |
| 1082 | struct ccw_dev_id id; |
| 1083 | |
| 1084 | ccw_device_get_id(cdev, &id); |
| 1085 | if (test_bit(id.devno, devs_no_auto[id.ssid])) |
| 1086 | return 0; |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
| 1090 | static void virtio_ccw_auto_online(void *data, async_cookie_t cookie) |
| 1091 | { |
| 1092 | struct ccw_device *cdev = data; |
| 1093 | int ret; |
| 1094 | |
| 1095 | ret = ccw_device_set_online(cdev); |
| 1096 | if (ret) |
| 1097 | dev_warn(&cdev->dev, "Failed to set online: %d\n", ret); |
| 1098 | } |
| 1099 | |
| 1100 | static int virtio_ccw_probe(struct ccw_device *cdev) |
| 1101 | { |
| 1102 | cdev->handler = virtio_ccw_int_handler; |
| 1103 | |
| 1104 | if (virtio_ccw_check_autoonline(cdev)) |
| 1105 | async_schedule(virtio_ccw_auto_online, cdev); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1109 | static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev) |
| 1110 | { |
| 1111 | unsigned long flags; |
| 1112 | struct virtio_ccw_device *vcdev; |
| 1113 | |
| 1114 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1115 | vcdev = dev_get_drvdata(&cdev->dev); |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1116 | if (!vcdev || vcdev->going_away) { |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1117 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1118 | return NULL; |
| 1119 | } |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1120 | vcdev->going_away = true; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1121 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1122 | return vcdev; |
| 1123 | } |
| 1124 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1125 | static void virtio_ccw_remove(struct ccw_device *cdev) |
| 1126 | { |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1127 | unsigned long flags; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1128 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1129 | |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1130 | if (vcdev && cdev->online) { |
| 1131 | if (vcdev->device_lost) |
| 1132 | virtio_break_device(&vcdev->vdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1133 | unregister_virtio_device(&vcdev->vdev); |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1134 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1135 | dev_set_drvdata(&cdev->dev, NULL); |
| 1136 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1137 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1138 | cdev->handler = NULL; |
| 1139 | } |
| 1140 | |
| 1141 | static int virtio_ccw_offline(struct ccw_device *cdev) |
| 1142 | { |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1143 | unsigned long flags; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1144 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1145 | |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1146 | if (!vcdev) |
| 1147 | return 0; |
| 1148 | if (vcdev->device_lost) |
| 1149 | virtio_break_device(&vcdev->vdev); |
| 1150 | unregister_virtio_device(&vcdev->vdev); |
| 1151 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1152 | dev_set_drvdata(&cdev->dev, NULL); |
| 1153 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1154 | return 0; |
| 1155 | } |
| 1156 | |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1157 | static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev) |
| 1158 | { |
| 1159 | struct virtio_rev_info *rev; |
| 1160 | struct ccw1 *ccw; |
| 1161 | int ret; |
| 1162 | |
| 1163 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 1164 | if (!ccw) |
| 1165 | return -ENOMEM; |
| 1166 | rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL); |
| 1167 | if (!rev) { |
| 1168 | kfree(ccw); |
| 1169 | return -ENOMEM; |
| 1170 | } |
| 1171 | |
| 1172 | /* Set transport revision */ |
| 1173 | ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV; |
| 1174 | ccw->flags = 0; |
| 1175 | ccw->count = sizeof(*rev); |
| 1176 | ccw->cda = (__u32)(unsigned long)rev; |
| 1177 | |
| 1178 | vcdev->revision = VIRTIO_CCW_REV_MAX; |
| 1179 | do { |
| 1180 | rev->revision = vcdev->revision; |
| 1181 | /* none of our supported revisions carry payload */ |
| 1182 | rev->length = 0; |
| 1183 | ret = ccw_io_helper(vcdev, ccw, |
| 1184 | VIRTIO_CCW_DOING_SET_VIRTIO_REV); |
| 1185 | if (ret == -EOPNOTSUPP) { |
| 1186 | if (vcdev->revision == 0) |
| 1187 | /* |
| 1188 | * The host device does not support setting |
| 1189 | * the revision: let's operate it in legacy |
| 1190 | * mode. |
| 1191 | */ |
| 1192 | ret = 0; |
| 1193 | else |
| 1194 | vcdev->revision--; |
| 1195 | } |
| 1196 | } while (ret == -EOPNOTSUPP); |
| 1197 | |
| 1198 | kfree(ccw); |
| 1199 | kfree(rev); |
| 1200 | return ret; |
| 1201 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1202 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1203 | static int virtio_ccw_online(struct ccw_device *cdev) |
| 1204 | { |
| 1205 | int ret; |
| 1206 | struct virtio_ccw_device *vcdev; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1207 | unsigned long flags; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1208 | |
| 1209 | vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL); |
| 1210 | if (!vcdev) { |
| 1211 | dev_warn(&cdev->dev, "Could not get memory for virtio\n"); |
| 1212 | ret = -ENOMEM; |
| 1213 | goto out_free; |
| 1214 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1215 | vcdev->config_block = kzalloc(sizeof(*vcdev->config_block), |
| 1216 | GFP_DMA | GFP_KERNEL); |
| 1217 | if (!vcdev->config_block) { |
| 1218 | ret = -ENOMEM; |
| 1219 | goto out_free; |
| 1220 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 1221 | vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL); |
| 1222 | if (!vcdev->status) { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1223 | ret = -ENOMEM; |
| 1224 | goto out_free; |
| 1225 | } |
| 1226 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1227 | vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */ |
| 1228 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1229 | vcdev->vdev.dev.parent = &cdev->dev; |
| 1230 | vcdev->vdev.dev.release = virtio_ccw_release_dev; |
| 1231 | vcdev->vdev.config = &virtio_ccw_config_ops; |
| 1232 | vcdev->cdev = cdev; |
| 1233 | init_waitqueue_head(&vcdev->wait_q); |
| 1234 | INIT_LIST_HEAD(&vcdev->virtqueues); |
| 1235 | spin_lock_init(&vcdev->lock); |
| 1236 | |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1237 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1238 | dev_set_drvdata(&cdev->dev, vcdev); |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1239 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1240 | vcdev->vdev.id.vendor = cdev->id.cu_type; |
| 1241 | vcdev->vdev.id.device = cdev->id.cu_model; |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1242 | |
Michael S. Tsirkin | 2003a7a | 2015-04-15 10:17:44 +0930 | [diff] [blame] | 1243 | ret = virtio_ccw_set_transport_rev(vcdev); |
| 1244 | if (ret) |
| 1245 | goto out_free; |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1246 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1247 | ret = register_virtio_device(&vcdev->vdev); |
| 1248 | if (ret) { |
| 1249 | dev_warn(&cdev->dev, "Failed to register virtio device: %d\n", |
| 1250 | ret); |
| 1251 | goto out_put; |
| 1252 | } |
| 1253 | return 0; |
| 1254 | out_put: |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1255 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1256 | dev_set_drvdata(&cdev->dev, NULL); |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1257 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1258 | put_device(&vcdev->vdev.dev); |
| 1259 | return ret; |
| 1260 | out_free: |
| 1261 | if (vcdev) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 1262 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1263 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1264 | } |
| 1265 | kfree(vcdev); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event) |
| 1270 | { |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1271 | int rc; |
| 1272 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 1273 | |
| 1274 | /* |
| 1275 | * Make sure vcdev is set |
| 1276 | * i.e. set_offline/remove callback not already running |
| 1277 | */ |
| 1278 | if (!vcdev) |
| 1279 | return NOTIFY_DONE; |
| 1280 | |
| 1281 | switch (event) { |
| 1282 | case CIO_GONE: |
| 1283 | vcdev->device_lost = true; |
| 1284 | rc = NOTIFY_DONE; |
| 1285 | break; |
| 1286 | default: |
| 1287 | rc = NOTIFY_DONE; |
| 1288 | break; |
| 1289 | } |
| 1290 | return rc; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | static struct ccw_device_id virtio_ids[] = { |
| 1294 | { CCW_DEVICE(0x3832, 0) }, |
| 1295 | {}, |
| 1296 | }; |
| 1297 | MODULE_DEVICE_TABLE(ccw, virtio_ids); |
| 1298 | |
| 1299 | static struct ccw_driver virtio_ccw_driver = { |
| 1300 | .driver = { |
| 1301 | .owner = THIS_MODULE, |
| 1302 | .name = "virtio_ccw", |
| 1303 | }, |
| 1304 | .ids = virtio_ids, |
| 1305 | .probe = virtio_ccw_probe, |
| 1306 | .remove = virtio_ccw_remove, |
| 1307 | .set_offline = virtio_ccw_offline, |
| 1308 | .set_online = virtio_ccw_online, |
| 1309 | .notify = virtio_ccw_cio_notify, |
Linus Torvalds | 89f8833 | 2013-02-24 13:07:18 -0800 | [diff] [blame] | 1310 | .int_class = IRQIO_VIR, |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1311 | }; |
| 1312 | |
| 1313 | static int __init pure_hex(char **cp, unsigned int *val, int min_digit, |
| 1314 | int max_digit, int max_val) |
| 1315 | { |
| 1316 | int diff; |
| 1317 | |
| 1318 | diff = 0; |
| 1319 | *val = 0; |
| 1320 | |
| 1321 | while (diff <= max_digit) { |
| 1322 | int value = hex_to_bin(**cp); |
| 1323 | |
| 1324 | if (value < 0) |
| 1325 | break; |
| 1326 | *val = *val * 16 + value; |
| 1327 | (*cp)++; |
| 1328 | diff++; |
| 1329 | } |
| 1330 | |
| 1331 | if ((diff < min_digit) || (diff > max_digit) || (*val > max_val)) |
| 1332 | return 1; |
| 1333 | |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | static int __init parse_busid(char *str, unsigned int *cssid, |
| 1338 | unsigned int *ssid, unsigned int *devno) |
| 1339 | { |
| 1340 | char *str_work; |
| 1341 | int rc, ret; |
| 1342 | |
| 1343 | rc = 1; |
| 1344 | |
| 1345 | if (*str == '\0') |
| 1346 | goto out; |
| 1347 | |
| 1348 | str_work = str; |
| 1349 | ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID); |
| 1350 | if (ret || (str_work[0] != '.')) |
| 1351 | goto out; |
| 1352 | str_work++; |
| 1353 | ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID); |
| 1354 | if (ret || (str_work[0] != '.')) |
| 1355 | goto out; |
| 1356 | str_work++; |
| 1357 | ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL); |
| 1358 | if (ret || (str_work[0] != '\0')) |
| 1359 | goto out; |
| 1360 | |
| 1361 | rc = 0; |
| 1362 | out: |
| 1363 | return rc; |
| 1364 | } |
| 1365 | |
| 1366 | static void __init no_auto_parse(void) |
| 1367 | { |
| 1368 | unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to; |
| 1369 | char *parm, *str; |
| 1370 | int rc; |
| 1371 | |
| 1372 | str = no_auto; |
| 1373 | while ((parm = strsep(&str, ","))) { |
| 1374 | rc = parse_busid(strsep(&parm, "-"), &from_cssid, |
| 1375 | &from_ssid, &from); |
| 1376 | if (rc) |
| 1377 | continue; |
| 1378 | if (parm != NULL) { |
| 1379 | rc = parse_busid(parm, &to_cssid, |
| 1380 | &to_ssid, &to); |
| 1381 | if ((from_ssid > to_ssid) || |
| 1382 | ((from_ssid == to_ssid) && (from > to))) |
| 1383 | rc = -EINVAL; |
| 1384 | } else { |
| 1385 | to_cssid = from_cssid; |
| 1386 | to_ssid = from_ssid; |
| 1387 | to = from; |
| 1388 | } |
| 1389 | if (rc) |
| 1390 | continue; |
| 1391 | while ((from_ssid < to_ssid) || |
| 1392 | ((from_ssid == to_ssid) && (from <= to))) { |
| 1393 | set_bit(from, devs_no_auto[from_ssid]); |
| 1394 | from++; |
| 1395 | if (from > __MAX_SUBCHANNEL) { |
| 1396 | from_ssid++; |
| 1397 | from = 0; |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | static int __init virtio_ccw_init(void) |
| 1404 | { |
| 1405 | /* parse no_auto string before we do anything further */ |
| 1406 | no_auto_parse(); |
| 1407 | return ccw_driver_register(&virtio_ccw_driver); |
| 1408 | } |
| 1409 | module_init(virtio_ccw_init); |
| 1410 | |
| 1411 | static void __exit virtio_ccw_exit(void) |
| 1412 | { |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1413 | int i; |
| 1414 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1415 | ccw_driver_unregister(&virtio_ccw_driver); |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1416 | for (i = 0; i < MAX_AIRQ_AREAS; i++) |
| 1417 | destroy_airq_info(airq_areas[i]); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1418 | } |
| 1419 | module_exit(virtio_ccw_exit); |