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 { |
| 345 | indicatorp = kmalloc(sizeof(&vcdev->indicators), |
| 346 | GFP_DMA | GFP_KERNEL); |
| 347 | if (!indicatorp) |
| 348 | return; |
| 349 | *indicatorp = 0; |
| 350 | ccw->cmd_code = CCW_CMD_SET_IND; |
| 351 | ccw->count = sizeof(vcdev->indicators); |
| 352 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 353 | } |
| 354 | /* Deregister indicators from host. */ |
| 355 | vcdev->indicators = 0; |
| 356 | ccw->flags = 0; |
| 357 | ret = ccw_io_helper(vcdev, ccw, |
| 358 | vcdev->is_thinint ? |
| 359 | VIRTIO_CCW_DOING_SET_IND_ADAPTER : |
| 360 | VIRTIO_CCW_DOING_SET_IND); |
| 361 | if (ret && (ret != -ENODEV)) |
| 362 | dev_info(&vcdev->cdev->dev, |
| 363 | "Failed to deregister indicators (%d)\n", ret); |
| 364 | else if (vcdev->is_thinint) |
| 365 | virtio_ccw_drop_indicators(vcdev); |
| 366 | kfree(indicatorp); |
| 367 | kfree(thinint_area); |
| 368 | } |
| 369 | |
Martin Schwidefsky | 1ec2772 | 2015-08-20 17:28:44 +0200 | [diff] [blame] | 370 | static inline long __do_kvm_notify(struct subchannel_id schid, |
| 371 | unsigned long queue_index, |
| 372 | long cookie) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 373 | { |
| 374 | register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY; |
| 375 | register struct subchannel_id __schid asm("2") = schid; |
| 376 | register unsigned long __index asm("3") = queue_index; |
| 377 | register long __rc asm("2"); |
Michael S. Tsirkin | 07e1693 | 2013-02-28 12:33:16 +0100 | [diff] [blame] | 378 | register long __cookie asm("4") = cookie; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 379 | |
| 380 | asm volatile ("diag 2,4,0x500\n" |
Michael S. Tsirkin | 07e1693 | 2013-02-28 12:33:16 +0100 | [diff] [blame] | 381 | : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index), |
| 382 | "d"(__cookie) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 383 | : "memory", "cc"); |
| 384 | return __rc; |
| 385 | } |
| 386 | |
Martin Schwidefsky | 1ec2772 | 2015-08-20 17:28:44 +0200 | [diff] [blame] | 387 | static inline long do_kvm_notify(struct subchannel_id schid, |
| 388 | unsigned long queue_index, |
| 389 | long cookie) |
| 390 | { |
| 391 | diag_stat_inc(DIAG_STAT_X500); |
| 392 | return __do_kvm_notify(schid, queue_index, cookie); |
| 393 | } |
| 394 | |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 395 | static bool virtio_ccw_kvm_notify(struct virtqueue *vq) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 396 | { |
| 397 | struct virtio_ccw_vq_info *info = vq->priv; |
| 398 | struct virtio_ccw_device *vcdev; |
| 399 | struct subchannel_id schid; |
| 400 | |
| 401 | vcdev = to_vc_device(info->vq->vdev); |
| 402 | ccw_device_get_schid(vcdev->cdev, &schid); |
Linus Torvalds | 01227a8 | 2013-05-05 14:47:31 -0700 | [diff] [blame] | 403 | info->cookie = do_kvm_notify(schid, vq->index, info->cookie); |
Heinz Graalfs | 46f9c2b | 2013-10-29 09:38:50 +1030 | [diff] [blame] | 404 | if (info->cookie < 0) |
| 405 | return false; |
| 406 | return true; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 409 | static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev, |
| 410 | struct ccw1 *ccw, int index) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 411 | { |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 412 | int ret; |
| 413 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 414 | vcdev->config_block->index = index; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 415 | ccw->cmd_code = CCW_CMD_READ_VQ_CONF; |
| 416 | ccw->flags = 0; |
| 417 | ccw->count = sizeof(struct vq_config_block); |
| 418 | ccw->cda = (__u32)(unsigned long)(vcdev->config_block); |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 419 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF); |
| 420 | if (ret) |
| 421 | return ret; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 422 | return vcdev->config_block->num; |
| 423 | } |
| 424 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 425 | static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 426 | { |
| 427 | struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev); |
| 428 | struct virtio_ccw_vq_info *info = vq->priv; |
| 429 | unsigned long flags; |
| 430 | unsigned long size; |
| 431 | int ret; |
Rusty Russell | 9d0ca6e | 2013-03-21 14:17:34 +0000 | [diff] [blame] | 432 | unsigned int index = vq->index; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 433 | |
| 434 | /* Remove from our list. */ |
| 435 | spin_lock_irqsave(&vcdev->lock, flags); |
| 436 | list_del(&info->node); |
| 437 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 438 | |
| 439 | /* Release from host. */ |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 440 | if (vcdev->revision == 0) { |
| 441 | info->info_block->l.queue = 0; |
| 442 | info->info_block->l.align = 0; |
| 443 | info->info_block->l.index = index; |
| 444 | info->info_block->l.num = 0; |
| 445 | ccw->count = sizeof(info->info_block->l); |
| 446 | } else { |
| 447 | info->info_block->s.desc = 0; |
| 448 | info->info_block->s.index = index; |
| 449 | info->info_block->s.num = 0; |
| 450 | info->info_block->s.avail = 0; |
| 451 | info->info_block->s.used = 0; |
| 452 | ccw->count = sizeof(info->info_block->s); |
| 453 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 454 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 455 | ccw->flags = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 456 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 457 | ret = ccw_io_helper(vcdev, ccw, |
| 458 | VIRTIO_CCW_DOING_SET_VQ | index); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 459 | /* |
| 460 | * -ENODEV isn't considered an error: The device is gone anyway. |
| 461 | * This may happen on device detach. |
| 462 | */ |
| 463 | if (ret && (ret != -ENODEV)) |
| 464 | dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d", |
| 465 | ret, index); |
| 466 | |
| 467 | vring_del_virtqueue(vq); |
| 468 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 469 | free_pages_exact(info->queue, size); |
| 470 | kfree(info->info_block); |
| 471 | kfree(info); |
| 472 | } |
| 473 | |
| 474 | static void virtio_ccw_del_vqs(struct virtio_device *vdev) |
| 475 | { |
| 476 | struct virtqueue *vq, *n; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 477 | struct ccw1 *ccw; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 478 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 479 | |
| 480 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 481 | if (!ccw) |
| 482 | return; |
| 483 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 484 | virtio_ccw_drop_indicator(vcdev, ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 485 | |
| 486 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 487 | virtio_ccw_del_vq(vq, ccw); |
| 488 | |
| 489 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev, |
| 493 | int i, vq_callback_t *callback, |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 494 | const char *name, |
| 495 | struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 496 | { |
| 497 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 498 | int err; |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 499 | struct virtqueue *vq = NULL; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 500 | struct virtio_ccw_vq_info *info; |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 501 | unsigned long size = 0; /* silence the compiler */ |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 502 | unsigned long flags; |
| 503 | |
| 504 | /* Allocate queue. */ |
| 505 | info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL); |
| 506 | if (!info) { |
| 507 | dev_warn(&vcdev->cdev->dev, "no info\n"); |
| 508 | err = -ENOMEM; |
| 509 | goto out_err; |
| 510 | } |
| 511 | info->info_block = kzalloc(sizeof(*info->info_block), |
| 512 | GFP_DMA | GFP_KERNEL); |
| 513 | if (!info->info_block) { |
| 514 | dev_warn(&vcdev->cdev->dev, "no info block\n"); |
| 515 | err = -ENOMEM; |
| 516 | goto out_err; |
| 517 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 518 | info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i); |
Pierre Morel | ad2aa04 | 2015-09-10 16:35:08 +0200 | [diff] [blame] | 519 | if (info->num < 0) { |
| 520 | err = info->num; |
| 521 | goto out_err; |
| 522 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 523 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 524 | info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| 525 | if (info->queue == NULL) { |
| 526 | dev_warn(&vcdev->cdev->dev, "no queue\n"); |
| 527 | err = -ENOMEM; |
| 528 | goto out_err; |
| 529 | } |
| 530 | |
| 531 | vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev, |
| 532 | true, info->queue, virtio_ccw_kvm_notify, |
| 533 | callback, name); |
| 534 | if (!vq) { |
| 535 | /* For now, we fail if we can't get the requested size. */ |
| 536 | dev_warn(&vcdev->cdev->dev, "no vq\n"); |
| 537 | err = -ENOMEM; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 538 | goto out_err; |
| 539 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 540 | |
| 541 | /* Register it with the host. */ |
Cornelia Huck | d467424 | 2014-10-07 16:39:51 +0200 | [diff] [blame] | 542 | if (vcdev->revision == 0) { |
| 543 | info->info_block->l.queue = (__u64)info->queue; |
| 544 | info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN; |
| 545 | info->info_block->l.index = i; |
| 546 | info->info_block->l.num = info->num; |
| 547 | ccw->count = sizeof(info->info_block->l); |
| 548 | } else { |
| 549 | info->info_block->s.desc = (__u64)info->queue; |
| 550 | info->info_block->s.index = i; |
| 551 | info->info_block->s.num = info->num; |
| 552 | info->info_block->s.avail = (__u64)virtqueue_get_avail(vq); |
| 553 | info->info_block->s.used = (__u64)virtqueue_get_used(vq); |
| 554 | ccw->count = sizeof(info->info_block->s); |
| 555 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 556 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 557 | ccw->flags = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 558 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 559 | err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 560 | if (err) { |
| 561 | dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n"); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 562 | goto out_err; |
| 563 | } |
| 564 | |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 565 | info->vq = vq; |
| 566 | vq->priv = info; |
| 567 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 568 | /* Save it to our list. */ |
| 569 | spin_lock_irqsave(&vcdev->lock, flags); |
| 570 | list_add(&info->node, &vcdev->virtqueues); |
| 571 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 572 | |
| 573 | return vq; |
| 574 | |
| 575 | out_err: |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 576 | if (vq) |
| 577 | vring_del_virtqueue(vq); |
| 578 | if (info) { |
| 579 | if (info->queue) |
| 580 | free_pages_exact(info->queue, size); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 581 | kfree(info->info_block); |
Cornelia Huck | c98d368 | 2013-01-25 15:34:16 +0100 | [diff] [blame] | 582 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 583 | kfree(info); |
| 584 | return ERR_PTR(err); |
| 585 | } |
| 586 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 587 | static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev, |
| 588 | struct virtqueue *vqs[], int nvqs, |
| 589 | struct ccw1 *ccw) |
| 590 | { |
| 591 | int ret; |
| 592 | struct virtio_thinint_area *thinint_area = NULL; |
| 593 | struct airq_info *info; |
| 594 | |
| 595 | thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL); |
| 596 | if (!thinint_area) { |
| 597 | ret = -ENOMEM; |
| 598 | goto out; |
| 599 | } |
| 600 | /* Try to get an indicator. */ |
| 601 | thinint_area->indicator = get_airq_indicator(vqs, nvqs, |
| 602 | &thinint_area->bit_nr, |
| 603 | &vcdev->airq_info); |
| 604 | if (!thinint_area->indicator) { |
| 605 | ret = -ENOSPC; |
| 606 | goto out; |
| 607 | } |
| 608 | info = vcdev->airq_info; |
| 609 | thinint_area->summary_indicator = |
| 610 | (unsigned long) &info->summary_indicator; |
| 611 | thinint_area->isc = VIRTIO_AIRQ_ISC; |
| 612 | ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER; |
| 613 | ccw->flags = CCW_FLAG_SLI; |
| 614 | ccw->count = sizeof(*thinint_area); |
| 615 | ccw->cda = (__u32)(unsigned long)thinint_area; |
| 616 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER); |
| 617 | if (ret) { |
| 618 | if (ret == -EOPNOTSUPP) { |
| 619 | /* |
| 620 | * The host does not support adapter interrupts |
| 621 | * for virtio-ccw, stop trying. |
| 622 | */ |
| 623 | virtio_ccw_use_airq = 0; |
| 624 | pr_info("Adapter interrupts unsupported on host\n"); |
| 625 | } else |
| 626 | dev_warn(&vcdev->cdev->dev, |
| 627 | "enabling adapter interrupts = %d\n", ret); |
| 628 | virtio_ccw_drop_indicators(vcdev); |
| 629 | } |
| 630 | out: |
| 631 | kfree(thinint_area); |
| 632 | return ret; |
| 633 | } |
| 634 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 635 | static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 636 | struct virtqueue *vqs[], |
| 637 | vq_callback_t *callbacks[], |
| 638 | const char *names[]) |
| 639 | { |
| 640 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 641 | unsigned long *indicatorp = NULL; |
| 642 | int ret, i; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 643 | struct ccw1 *ccw; |
| 644 | |
| 645 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 646 | if (!ccw) |
| 647 | return -ENOMEM; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 648 | |
| 649 | for (i = 0; i < nvqs; ++i) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 650 | vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i], |
| 651 | ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 652 | if (IS_ERR(vqs[i])) { |
| 653 | ret = PTR_ERR(vqs[i]); |
| 654 | vqs[i] = NULL; |
| 655 | goto out; |
| 656 | } |
| 657 | } |
| 658 | ret = -ENOMEM; |
| 659 | /* We need a data area under 2G to communicate. */ |
| 660 | indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL); |
| 661 | if (!indicatorp) |
| 662 | goto out; |
| 663 | *indicatorp = (unsigned long) &vcdev->indicators; |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 664 | if (vcdev->is_thinint) { |
| 665 | ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw); |
| 666 | if (ret) |
| 667 | /* no error, just fall back to legacy interrupts */ |
| 668 | vcdev->is_thinint = 0; |
| 669 | } |
| 670 | if (!vcdev->is_thinint) { |
| 671 | /* Register queue indicators with host. */ |
| 672 | vcdev->indicators = 0; |
| 673 | ccw->cmd_code = CCW_CMD_SET_IND; |
| 674 | ccw->flags = 0; |
| 675 | ccw->count = sizeof(vcdev->indicators); |
| 676 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 677 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND); |
| 678 | if (ret) |
| 679 | goto out; |
| 680 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 681 | /* Register indicators2 with host for config changes */ |
| 682 | *indicatorp = (unsigned long) &vcdev->indicators2; |
| 683 | vcdev->indicators2 = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 684 | ccw->cmd_code = CCW_CMD_SET_CONF_IND; |
| 685 | ccw->flags = 0; |
| 686 | ccw->count = sizeof(vcdev->indicators2); |
| 687 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 688 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 689 | if (ret) |
| 690 | goto out; |
| 691 | |
| 692 | kfree(indicatorp); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 693 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 694 | return 0; |
| 695 | out: |
| 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 | virtio_ccw_del_vqs(vdev); |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | static void virtio_ccw_reset(struct virtio_device *vdev) |
| 703 | { |
| 704 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 705 | struct ccw1 *ccw; |
| 706 | |
| 707 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 708 | if (!ccw) |
| 709 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 710 | |
| 711 | /* Zero status bits. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 712 | *vcdev->status = 0; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 713 | |
| 714 | /* Send a reset ccw on device. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 715 | ccw->cmd_code = CCW_CMD_VDEV_RESET; |
| 716 | ccw->flags = 0; |
| 717 | ccw->count = 0; |
| 718 | ccw->cda = 0; |
| 719 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET); |
| 720 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Michael S. Tsirkin | d025477 | 2014-10-07 16:39:43 +0200 | [diff] [blame] | 723 | static u64 virtio_ccw_get_features(struct virtio_device *vdev) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 724 | { |
| 725 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 726 | struct virtio_feature_desc *features; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 727 | int ret; |
| 728 | u64 rc; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 729 | struct ccw1 *ccw; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 730 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 731 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 732 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 733 | return 0; |
| 734 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 735 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
| 736 | if (!features) { |
| 737 | rc = 0; |
| 738 | goto out_free; |
| 739 | } |
| 740 | /* Read the feature bits from the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 741 | features->index = 0; |
| 742 | ccw->cmd_code = CCW_CMD_READ_FEAT; |
| 743 | ccw->flags = 0; |
| 744 | ccw->count = sizeof(*features); |
| 745 | ccw->cda = (__u32)(unsigned long)features; |
| 746 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
| 747 | if (ret) { |
| 748 | rc = 0; |
| 749 | goto out_free; |
| 750 | } |
| 751 | |
| 752 | rc = le32_to_cpu(features->features); |
| 753 | |
Michael S. Tsirkin | ce15408 | 2014-12-04 18:59:50 +0200 | [diff] [blame] | 754 | if (vcdev->revision == 0) |
| 755 | goto out_free; |
| 756 | |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 757 | /* Read second half of the feature bits from the host. */ |
| 758 | features->index = 1; |
| 759 | ccw->cmd_code = CCW_CMD_READ_FEAT; |
| 760 | ccw->flags = 0; |
| 761 | ccw->count = sizeof(*features); |
| 762 | ccw->cda = (__u32)(unsigned long)features; |
| 763 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
| 764 | if (ret == 0) |
| 765 | rc |= (u64)le32_to_cpu(features->features) << 32; |
| 766 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 767 | out_free: |
| 768 | kfree(features); |
| 769 | kfree(ccw); |
| 770 | return rc; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 773 | static int virtio_ccw_finalize_features(struct virtio_device *vdev) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 774 | { |
| 775 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 776 | struct virtio_feature_desc *features; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 777 | struct ccw1 *ccw; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 778 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 779 | |
Michael S. Tsirkin | f13d8bc | 2014-12-09 14:46:44 +0200 | [diff] [blame] | 780 | if (vcdev->revision >= 1 && |
Michael S. Tsirkin | 4d23676 | 2014-12-04 19:16:43 +0200 | [diff] [blame] | 781 | !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) { |
| 782 | dev_err(&vdev->dev, "virtio: device uses revision 1 " |
| 783 | "but does not have VIRTIO_F_VERSION_1\n"); |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 787 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 788 | if (!ccw) |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 789 | return -ENOMEM; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 790 | |
| 791 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 792 | if (!features) { |
| 793 | ret = -ENOMEM; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 794 | goto out_free; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 795 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 796 | /* Give virtio_ring a chance to accept features. */ |
| 797 | vring_transport_features(vdev); |
| 798 | |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 799 | features->index = 0; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 800 | features->features = cpu_to_le32((u32)vdev->features); |
| 801 | /* Write the first half of the feature bits to the host. */ |
| 802 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
| 803 | ccw->flags = 0; |
| 804 | ccw->count = sizeof(*features); |
| 805 | ccw->cda = (__u32)(unsigned long)features; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 806 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
| 807 | if (ret) |
| 808 | goto out_free; |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 809 | |
Michael S. Tsirkin | ce15408 | 2014-12-04 18:59:50 +0200 | [diff] [blame] | 810 | if (vcdev->revision == 0) |
| 811 | goto out_free; |
| 812 | |
Michael S. Tsirkin | 732c56e | 2014-11-27 13:54:28 +0200 | [diff] [blame] | 813 | features->index = 1; |
| 814 | features->features = cpu_to_le32(vdev->features >> 32); |
| 815 | /* Write the second half of the feature bits to the host. */ |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 816 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
| 817 | ccw->flags = 0; |
| 818 | ccw->count = sizeof(*features); |
| 819 | ccw->cda = (__u32)(unsigned long)features; |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 820 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
Michael S. Tsirkin | e16e12b | 2014-10-07 16:39:42 +0200 | [diff] [blame] | 821 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 822 | out_free: |
| 823 | kfree(features); |
| 824 | kfree(ccw); |
Michael S. Tsirkin | 5c609a5 | 2014-12-04 20:20:27 +0200 | [diff] [blame] | 825 | |
Cornelia Huck | f01a2a8 | 2014-12-09 11:46:59 +0100 | [diff] [blame] | 826 | return ret; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static void virtio_ccw_get_config(struct virtio_device *vdev, |
| 830 | unsigned int offset, void *buf, unsigned len) |
| 831 | { |
| 832 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 833 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 834 | struct ccw1 *ccw; |
| 835 | void *config_area; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 836 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 837 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 838 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 839 | return; |
| 840 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 841 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 842 | if (!config_area) |
| 843 | goto out_free; |
| 844 | |
| 845 | /* Read the config area from the host. */ |
| 846 | ccw->cmd_code = CCW_CMD_READ_CONF; |
| 847 | ccw->flags = 0; |
| 848 | ccw->count = offset + len; |
| 849 | ccw->cda = (__u32)(unsigned long)config_area; |
| 850 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG); |
| 851 | if (ret) |
| 852 | goto out_free; |
| 853 | |
Cornelia Huck | 431dae7 | 2015-06-29 16:44:01 +0200 | [diff] [blame] | 854 | memcpy(vcdev->config, config_area, offset + len); |
| 855 | if (buf) |
| 856 | memcpy(buf, &vcdev->config[offset], len); |
| 857 | if (vcdev->config_ready < offset + len) |
| 858 | vcdev->config_ready = offset + len; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 859 | |
| 860 | out_free: |
| 861 | kfree(config_area); |
| 862 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void virtio_ccw_set_config(struct virtio_device *vdev, |
| 866 | unsigned int offset, const void *buf, |
| 867 | unsigned len) |
| 868 | { |
| 869 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 870 | struct ccw1 *ccw; |
| 871 | void *config_area; |
| 872 | |
| 873 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 874 | if (!ccw) |
| 875 | return; |
| 876 | |
| 877 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 878 | if (!config_area) |
| 879 | goto out_free; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 880 | |
Cornelia Huck | 431dae7 | 2015-06-29 16:44:01 +0200 | [diff] [blame] | 881 | /* Make sure we don't overwrite fields. */ |
| 882 | if (vcdev->config_ready < offset) |
| 883 | virtio_ccw_get_config(vdev, 0, NULL, offset); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 884 | memcpy(&vcdev->config[offset], buf, len); |
| 885 | /* Write the config area to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 886 | memcpy(config_area, vcdev->config, sizeof(vcdev->config)); |
| 887 | ccw->cmd_code = CCW_CMD_WRITE_CONF; |
| 888 | ccw->flags = 0; |
| 889 | ccw->count = offset + len; |
| 890 | ccw->cda = (__u32)(unsigned long)config_area; |
| 891 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG); |
| 892 | |
| 893 | out_free: |
| 894 | kfree(config_area); |
| 895 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | static u8 virtio_ccw_get_status(struct virtio_device *vdev) |
| 899 | { |
| 900 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 901 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 902 | return *vcdev->status; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status) |
| 906 | { |
| 907 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 908 | u8 old_status = *vcdev->status; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 909 | struct ccw1 *ccw; |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 910 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 911 | |
| 912 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 913 | if (!ccw) |
| 914 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 915 | |
| 916 | /* Write the status to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 917 | *vcdev->status = status; |
| 918 | ccw->cmd_code = CCW_CMD_WRITE_STATUS; |
| 919 | ccw->flags = 0; |
| 920 | ccw->count = sizeof(status); |
| 921 | ccw->cda = (__u32)(unsigned long)vcdev->status; |
Michael S. Tsirkin | 4855513 | 2014-10-23 18:40:30 +0300 | [diff] [blame] | 922 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS); |
| 923 | /* Write failed? We assume status is unchanged. */ |
| 924 | if (ret) |
| 925 | *vcdev->status = old_status; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 926 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static struct virtio_config_ops virtio_ccw_config_ops = { |
| 930 | .get_features = virtio_ccw_get_features, |
| 931 | .finalize_features = virtio_ccw_finalize_features, |
| 932 | .get = virtio_ccw_get_config, |
| 933 | .set = virtio_ccw_set_config, |
| 934 | .get_status = virtio_ccw_get_status, |
| 935 | .set_status = virtio_ccw_set_status, |
| 936 | .reset = virtio_ccw_reset, |
| 937 | .find_vqs = virtio_ccw_find_vqs, |
| 938 | .del_vqs = virtio_ccw_del_vqs, |
| 939 | }; |
| 940 | |
| 941 | |
| 942 | /* |
| 943 | * ccw bus driver related functions |
| 944 | */ |
| 945 | |
| 946 | static void virtio_ccw_release_dev(struct device *_d) |
| 947 | { |
| 948 | struct virtio_device *dev = container_of(_d, struct virtio_device, |
| 949 | dev); |
| 950 | struct virtio_ccw_device *vcdev = to_vc_device(dev); |
| 951 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 952 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 953 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 954 | kfree(vcdev); |
| 955 | } |
| 956 | |
| 957 | static int irb_is_error(struct irb *irb) |
| 958 | { |
| 959 | if (scsw_cstat(&irb->scsw) != 0) |
| 960 | return 1; |
| 961 | if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) |
| 962 | return 1; |
| 963 | if (scsw_cc(&irb->scsw) != 0) |
| 964 | return 1; |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev, |
| 969 | int index) |
| 970 | { |
| 971 | struct virtio_ccw_vq_info *info; |
| 972 | unsigned long flags; |
| 973 | struct virtqueue *vq; |
| 974 | |
| 975 | vq = NULL; |
| 976 | spin_lock_irqsave(&vcdev->lock, flags); |
| 977 | list_for_each_entry(info, &vcdev->virtqueues, node) { |
Rusty Russell | 9d0ca6e | 2013-03-21 14:17:34 +0000 | [diff] [blame] | 978 | if (info->vq->index == index) { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 979 | vq = info->vq; |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 984 | return vq; |
| 985 | } |
| 986 | |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame^] | 987 | static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev, |
| 988 | __u32 activity) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 989 | { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 990 | if (vcdev->curr_io & activity) { |
| 991 | switch (activity) { |
| 992 | case VIRTIO_CCW_DOING_READ_FEAT: |
| 993 | case VIRTIO_CCW_DOING_WRITE_FEAT: |
| 994 | case VIRTIO_CCW_DOING_READ_CONFIG: |
| 995 | case VIRTIO_CCW_DOING_WRITE_CONFIG: |
| 996 | case VIRTIO_CCW_DOING_WRITE_STATUS: |
| 997 | case VIRTIO_CCW_DOING_SET_VQ: |
| 998 | case VIRTIO_CCW_DOING_SET_IND: |
| 999 | case VIRTIO_CCW_DOING_SET_CONF_IND: |
| 1000 | case VIRTIO_CCW_DOING_RESET: |
| 1001 | case VIRTIO_CCW_DOING_READ_VQ_CONF: |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1002 | case VIRTIO_CCW_DOING_SET_IND_ADAPTER: |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1003 | case VIRTIO_CCW_DOING_SET_VIRTIO_REV: |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1004 | vcdev->curr_io &= ~activity; |
| 1005 | wake_up(&vcdev->wait_q); |
| 1006 | break; |
| 1007 | default: |
| 1008 | /* don't know what to do... */ |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame^] | 1009 | dev_warn(&vcdev->cdev->dev, |
| 1010 | "Suspicious activity '%08x'\n", activity); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1011 | WARN_ON(1); |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
Cornelia Huck | 74a599f | 2015-12-03 17:24:00 +0100 | [diff] [blame^] | 1015 | } |
| 1016 | |
| 1017 | static void virtio_ccw_int_handler(struct ccw_device *cdev, |
| 1018 | unsigned long intparm, |
| 1019 | struct irb *irb) |
| 1020 | { |
| 1021 | __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK; |
| 1022 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 1023 | int i; |
| 1024 | struct virtqueue *vq; |
| 1025 | |
| 1026 | if (!vcdev) |
| 1027 | return; |
| 1028 | if (IS_ERR(irb)) { |
| 1029 | vcdev->err = PTR_ERR(irb); |
| 1030 | virtio_ccw_check_activity(vcdev, activity); |
| 1031 | /* Don't poke around indicators, something's wrong. */ |
| 1032 | return; |
| 1033 | } |
| 1034 | /* Check if it's a notification from the host. */ |
| 1035 | if ((intparm == 0) && |
| 1036 | (scsw_stctl(&irb->scsw) == |
| 1037 | (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) { |
| 1038 | /* OK */ |
| 1039 | } |
| 1040 | if (irb_is_error(irb)) { |
| 1041 | /* Command reject? */ |
| 1042 | if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) && |
| 1043 | (irb->ecw[0] & SNS0_CMD_REJECT)) |
| 1044 | vcdev->err = -EOPNOTSUPP; |
| 1045 | else |
| 1046 | /* Map everything else to -EIO. */ |
| 1047 | vcdev->err = -EIO; |
| 1048 | } |
| 1049 | virtio_ccw_check_activity(vcdev, activity); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1050 | for_each_set_bit(i, &vcdev->indicators, |
| 1051 | sizeof(vcdev->indicators) * BITS_PER_BYTE) { |
| 1052 | /* The bit clear must happen before the vring kick. */ |
| 1053 | clear_bit(i, &vcdev->indicators); |
| 1054 | barrier(); |
| 1055 | vq = virtio_ccw_vq_by_ind(vcdev, i); |
| 1056 | vring_interrupt(0, vq); |
| 1057 | } |
| 1058 | if (test_bit(0, &vcdev->indicators2)) { |
Michael S. Tsirkin | 016c98c | 2014-10-14 10:40:34 +1030 | [diff] [blame] | 1059 | virtio_config_changed(&vcdev->vdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1060 | clear_bit(0, &vcdev->indicators2); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | * We usually want to autoonline all devices, but give the admin |
| 1066 | * a way to exempt devices from this. |
| 1067 | */ |
| 1068 | #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \ |
| 1069 | (8*sizeof(long))) |
| 1070 | static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS]; |
| 1071 | |
| 1072 | static char *no_auto = ""; |
| 1073 | |
| 1074 | module_param(no_auto, charp, 0444); |
| 1075 | MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined"); |
| 1076 | |
| 1077 | static int virtio_ccw_check_autoonline(struct ccw_device *cdev) |
| 1078 | { |
| 1079 | struct ccw_dev_id id; |
| 1080 | |
| 1081 | ccw_device_get_id(cdev, &id); |
| 1082 | if (test_bit(id.devno, devs_no_auto[id.ssid])) |
| 1083 | return 0; |
| 1084 | return 1; |
| 1085 | } |
| 1086 | |
| 1087 | static void virtio_ccw_auto_online(void *data, async_cookie_t cookie) |
| 1088 | { |
| 1089 | struct ccw_device *cdev = data; |
| 1090 | int ret; |
| 1091 | |
| 1092 | ret = ccw_device_set_online(cdev); |
| 1093 | if (ret) |
| 1094 | dev_warn(&cdev->dev, "Failed to set online: %d\n", ret); |
| 1095 | } |
| 1096 | |
| 1097 | static int virtio_ccw_probe(struct ccw_device *cdev) |
| 1098 | { |
| 1099 | cdev->handler = virtio_ccw_int_handler; |
| 1100 | |
| 1101 | if (virtio_ccw_check_autoonline(cdev)) |
| 1102 | async_schedule(virtio_ccw_auto_online, cdev); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1106 | static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev) |
| 1107 | { |
| 1108 | unsigned long flags; |
| 1109 | struct virtio_ccw_device *vcdev; |
| 1110 | |
| 1111 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1112 | vcdev = dev_get_drvdata(&cdev->dev); |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1113 | if (!vcdev || vcdev->going_away) { |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1114 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1115 | return NULL; |
| 1116 | } |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1117 | vcdev->going_away = true; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1118 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1119 | return vcdev; |
| 1120 | } |
| 1121 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1122 | static void virtio_ccw_remove(struct ccw_device *cdev) |
| 1123 | { |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1124 | unsigned long flags; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1125 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1126 | |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1127 | if (vcdev && cdev->online) { |
| 1128 | if (vcdev->device_lost) |
| 1129 | virtio_break_device(&vcdev->vdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1130 | unregister_virtio_device(&vcdev->vdev); |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1131 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1132 | dev_set_drvdata(&cdev->dev, NULL); |
| 1133 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
| 1134 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1135 | cdev->handler = NULL; |
| 1136 | } |
| 1137 | |
| 1138 | static int virtio_ccw_offline(struct ccw_device *cdev) |
| 1139 | { |
Heinz Graalfs | 79629b2 | 2014-03-05 15:23:54 +0100 | [diff] [blame] | 1140 | unsigned long flags; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1141 | struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1142 | |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1143 | if (!vcdev) |
| 1144 | return 0; |
| 1145 | if (vcdev->device_lost) |
| 1146 | virtio_break_device(&vcdev->vdev); |
| 1147 | unregister_virtio_device(&vcdev->vdev); |
| 1148 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
| 1149 | dev_set_drvdata(&cdev->dev, NULL); |
| 1150 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1151 | return 0; |
| 1152 | } |
| 1153 | |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1154 | static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev) |
| 1155 | { |
| 1156 | struct virtio_rev_info *rev; |
| 1157 | struct ccw1 *ccw; |
| 1158 | int ret; |
| 1159 | |
| 1160 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 1161 | if (!ccw) |
| 1162 | return -ENOMEM; |
| 1163 | rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL); |
| 1164 | if (!rev) { |
| 1165 | kfree(ccw); |
| 1166 | return -ENOMEM; |
| 1167 | } |
| 1168 | |
| 1169 | /* Set transport revision */ |
| 1170 | ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV; |
| 1171 | ccw->flags = 0; |
| 1172 | ccw->count = sizeof(*rev); |
| 1173 | ccw->cda = (__u32)(unsigned long)rev; |
| 1174 | |
| 1175 | vcdev->revision = VIRTIO_CCW_REV_MAX; |
| 1176 | do { |
| 1177 | rev->revision = vcdev->revision; |
| 1178 | /* none of our supported revisions carry payload */ |
| 1179 | rev->length = 0; |
| 1180 | ret = ccw_io_helper(vcdev, ccw, |
| 1181 | VIRTIO_CCW_DOING_SET_VIRTIO_REV); |
| 1182 | if (ret == -EOPNOTSUPP) { |
| 1183 | if (vcdev->revision == 0) |
| 1184 | /* |
| 1185 | * The host device does not support setting |
| 1186 | * the revision: let's operate it in legacy |
| 1187 | * mode. |
| 1188 | */ |
| 1189 | ret = 0; |
| 1190 | else |
| 1191 | vcdev->revision--; |
| 1192 | } |
| 1193 | } while (ret == -EOPNOTSUPP); |
| 1194 | |
| 1195 | kfree(ccw); |
| 1196 | kfree(rev); |
| 1197 | return ret; |
| 1198 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1199 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1200 | static int virtio_ccw_online(struct ccw_device *cdev) |
| 1201 | { |
| 1202 | int ret; |
| 1203 | struct virtio_ccw_device *vcdev; |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1204 | unsigned long flags; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1205 | |
| 1206 | vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL); |
| 1207 | if (!vcdev) { |
| 1208 | dev_warn(&cdev->dev, "Could not get memory for virtio\n"); |
| 1209 | ret = -ENOMEM; |
| 1210 | goto out_free; |
| 1211 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1212 | vcdev->config_block = kzalloc(sizeof(*vcdev->config_block), |
| 1213 | GFP_DMA | GFP_KERNEL); |
| 1214 | if (!vcdev->config_block) { |
| 1215 | ret = -ENOMEM; |
| 1216 | goto out_free; |
| 1217 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 1218 | vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL); |
| 1219 | if (!vcdev->status) { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1220 | ret = -ENOMEM; |
| 1221 | goto out_free; |
| 1222 | } |
| 1223 | |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1224 | vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */ |
| 1225 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1226 | vcdev->vdev.dev.parent = &cdev->dev; |
| 1227 | vcdev->vdev.dev.release = virtio_ccw_release_dev; |
| 1228 | vcdev->vdev.config = &virtio_ccw_config_ops; |
| 1229 | vcdev->cdev = cdev; |
| 1230 | init_waitqueue_head(&vcdev->wait_q); |
| 1231 | INIT_LIST_HEAD(&vcdev->virtqueues); |
| 1232 | spin_lock_init(&vcdev->lock); |
| 1233 | |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1234 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1235 | dev_set_drvdata(&cdev->dev, vcdev); |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1236 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1237 | vcdev->vdev.id.vendor = cdev->id.cu_type; |
| 1238 | vcdev->vdev.id.device = cdev->id.cu_model; |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1239 | |
Michael S. Tsirkin | 2003a7a | 2015-04-15 10:17:44 +0930 | [diff] [blame] | 1240 | ret = virtio_ccw_set_transport_rev(vcdev); |
| 1241 | if (ret) |
| 1242 | goto out_free; |
Thomas Huth | 6bb2c83 | 2014-10-07 16:39:50 +0200 | [diff] [blame] | 1243 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1244 | ret = register_virtio_device(&vcdev->vdev); |
| 1245 | if (ret) { |
| 1246 | dev_warn(&cdev->dev, "Failed to register virtio device: %d\n", |
| 1247 | ret); |
| 1248 | goto out_put; |
| 1249 | } |
| 1250 | return 0; |
| 1251 | out_put: |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1252 | spin_lock_irqsave(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1253 | dev_set_drvdata(&cdev->dev, NULL); |
Heinz Graalfs | 2e02104 | 2014-02-27 14:34:35 +0100 | [diff] [blame] | 1254 | spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1255 | put_device(&vcdev->vdev.dev); |
| 1256 | return ret; |
| 1257 | out_free: |
| 1258 | if (vcdev) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 1259 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1260 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1261 | } |
| 1262 | kfree(vcdev); |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
| 1266 | static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event) |
| 1267 | { |
Heinz Graalfs | e75279c | 2014-04-28 11:24:05 +0930 | [diff] [blame] | 1268 | int rc; |
| 1269 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 1270 | |
| 1271 | /* |
| 1272 | * Make sure vcdev is set |
| 1273 | * i.e. set_offline/remove callback not already running |
| 1274 | */ |
| 1275 | if (!vcdev) |
| 1276 | return NOTIFY_DONE; |
| 1277 | |
| 1278 | switch (event) { |
| 1279 | case CIO_GONE: |
| 1280 | vcdev->device_lost = true; |
| 1281 | rc = NOTIFY_DONE; |
| 1282 | break; |
| 1283 | default: |
| 1284 | rc = NOTIFY_DONE; |
| 1285 | break; |
| 1286 | } |
| 1287 | return rc; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | static struct ccw_device_id virtio_ids[] = { |
| 1291 | { CCW_DEVICE(0x3832, 0) }, |
| 1292 | {}, |
| 1293 | }; |
| 1294 | MODULE_DEVICE_TABLE(ccw, virtio_ids); |
| 1295 | |
| 1296 | static struct ccw_driver virtio_ccw_driver = { |
| 1297 | .driver = { |
| 1298 | .owner = THIS_MODULE, |
| 1299 | .name = "virtio_ccw", |
| 1300 | }, |
| 1301 | .ids = virtio_ids, |
| 1302 | .probe = virtio_ccw_probe, |
| 1303 | .remove = virtio_ccw_remove, |
| 1304 | .set_offline = virtio_ccw_offline, |
| 1305 | .set_online = virtio_ccw_online, |
| 1306 | .notify = virtio_ccw_cio_notify, |
Linus Torvalds | 89f8833 | 2013-02-24 13:07:18 -0800 | [diff] [blame] | 1307 | .int_class = IRQIO_VIR, |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1308 | }; |
| 1309 | |
| 1310 | static int __init pure_hex(char **cp, unsigned int *val, int min_digit, |
| 1311 | int max_digit, int max_val) |
| 1312 | { |
| 1313 | int diff; |
| 1314 | |
| 1315 | diff = 0; |
| 1316 | *val = 0; |
| 1317 | |
| 1318 | while (diff <= max_digit) { |
| 1319 | int value = hex_to_bin(**cp); |
| 1320 | |
| 1321 | if (value < 0) |
| 1322 | break; |
| 1323 | *val = *val * 16 + value; |
| 1324 | (*cp)++; |
| 1325 | diff++; |
| 1326 | } |
| 1327 | |
| 1328 | if ((diff < min_digit) || (diff > max_digit) || (*val > max_val)) |
| 1329 | return 1; |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | static int __init parse_busid(char *str, unsigned int *cssid, |
| 1335 | unsigned int *ssid, unsigned int *devno) |
| 1336 | { |
| 1337 | char *str_work; |
| 1338 | int rc, ret; |
| 1339 | |
| 1340 | rc = 1; |
| 1341 | |
| 1342 | if (*str == '\0') |
| 1343 | goto out; |
| 1344 | |
| 1345 | str_work = str; |
| 1346 | ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID); |
| 1347 | if (ret || (str_work[0] != '.')) |
| 1348 | goto out; |
| 1349 | str_work++; |
| 1350 | ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID); |
| 1351 | if (ret || (str_work[0] != '.')) |
| 1352 | goto out; |
| 1353 | str_work++; |
| 1354 | ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL); |
| 1355 | if (ret || (str_work[0] != '\0')) |
| 1356 | goto out; |
| 1357 | |
| 1358 | rc = 0; |
| 1359 | out: |
| 1360 | return rc; |
| 1361 | } |
| 1362 | |
| 1363 | static void __init no_auto_parse(void) |
| 1364 | { |
| 1365 | unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to; |
| 1366 | char *parm, *str; |
| 1367 | int rc; |
| 1368 | |
| 1369 | str = no_auto; |
| 1370 | while ((parm = strsep(&str, ","))) { |
| 1371 | rc = parse_busid(strsep(&parm, "-"), &from_cssid, |
| 1372 | &from_ssid, &from); |
| 1373 | if (rc) |
| 1374 | continue; |
| 1375 | if (parm != NULL) { |
| 1376 | rc = parse_busid(parm, &to_cssid, |
| 1377 | &to_ssid, &to); |
| 1378 | if ((from_ssid > to_ssid) || |
| 1379 | ((from_ssid == to_ssid) && (from > to))) |
| 1380 | rc = -EINVAL; |
| 1381 | } else { |
| 1382 | to_cssid = from_cssid; |
| 1383 | to_ssid = from_ssid; |
| 1384 | to = from; |
| 1385 | } |
| 1386 | if (rc) |
| 1387 | continue; |
| 1388 | while ((from_ssid < to_ssid) || |
| 1389 | ((from_ssid == to_ssid) && (from <= to))) { |
| 1390 | set_bit(from, devs_no_auto[from_ssid]); |
| 1391 | from++; |
| 1392 | if (from > __MAX_SUBCHANNEL) { |
| 1393 | from_ssid++; |
| 1394 | from = 0; |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | static int __init virtio_ccw_init(void) |
| 1401 | { |
| 1402 | /* parse no_auto string before we do anything further */ |
| 1403 | no_auto_parse(); |
| 1404 | return ccw_driver_register(&virtio_ccw_driver); |
| 1405 | } |
| 1406 | module_init(virtio_ccw_init); |
| 1407 | |
| 1408 | static void __exit virtio_ccw_exit(void) |
| 1409 | { |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1410 | int i; |
| 1411 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1412 | ccw_driver_unregister(&virtio_ccw_driver); |
Cornelia Huck | 96b1453 | 2013-02-06 10:23:39 +0100 | [diff] [blame] | 1413 | for (i = 0; i < MAX_AIRQ_AREAS; i++) |
| 1414 | destroy_airq_info(airq_areas[i]); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1415 | } |
| 1416 | module_exit(virtio_ccw_exit); |