Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ccw based virtio transport |
| 3 | * |
| 4 | * Copyright IBM Corp. 2012 |
| 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> |
| 30 | #include <asm/setup.h> |
| 31 | #include <asm/irq.h> |
| 32 | #include <asm/cio.h> |
| 33 | #include <asm/ccwdev.h> |
| 34 | |
| 35 | /* |
| 36 | * virtio related functions |
| 37 | */ |
| 38 | |
| 39 | struct vq_config_block { |
| 40 | __u16 index; |
| 41 | __u16 num; |
| 42 | } __packed; |
| 43 | |
| 44 | #define VIRTIO_CCW_CONFIG_SIZE 0x100 |
| 45 | /* same as PCI config space size, should be enough for all drivers */ |
| 46 | |
| 47 | struct virtio_ccw_device { |
| 48 | struct virtio_device vdev; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 49 | __u8 *status; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 50 | __u8 config[VIRTIO_CCW_CONFIG_SIZE]; |
| 51 | struct ccw_device *cdev; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 52 | __u32 curr_io; |
| 53 | int err; |
| 54 | wait_queue_head_t wait_q; |
| 55 | spinlock_t lock; |
| 56 | struct list_head virtqueues; |
| 57 | unsigned long indicators; |
| 58 | unsigned long indicators2; |
| 59 | struct vq_config_block *config_block; |
| 60 | }; |
| 61 | |
| 62 | struct vq_info_block { |
| 63 | __u64 queue; |
| 64 | __u32 align; |
| 65 | __u16 index; |
| 66 | __u16 num; |
| 67 | } __packed; |
| 68 | |
| 69 | struct virtio_feature_desc { |
| 70 | __u32 features; |
| 71 | __u8 index; |
| 72 | } __packed; |
| 73 | |
| 74 | struct virtio_ccw_vq_info { |
| 75 | struct virtqueue *vq; |
| 76 | int num; |
| 77 | void *queue; |
| 78 | struct vq_info_block *info_block; |
| 79 | struct list_head node; |
| 80 | }; |
| 81 | |
| 82 | #define KVM_VIRTIO_CCW_RING_ALIGN 4096 |
| 83 | |
| 84 | #define KVM_S390_VIRTIO_CCW_NOTIFY 3 |
| 85 | |
| 86 | #define CCW_CMD_SET_VQ 0x13 |
| 87 | #define CCW_CMD_VDEV_RESET 0x33 |
| 88 | #define CCW_CMD_SET_IND 0x43 |
| 89 | #define CCW_CMD_SET_CONF_IND 0x53 |
| 90 | #define CCW_CMD_READ_FEAT 0x12 |
| 91 | #define CCW_CMD_WRITE_FEAT 0x11 |
| 92 | #define CCW_CMD_READ_CONF 0x22 |
| 93 | #define CCW_CMD_WRITE_CONF 0x21 |
| 94 | #define CCW_CMD_WRITE_STATUS 0x31 |
| 95 | #define CCW_CMD_READ_VQ_CONF 0x32 |
| 96 | |
| 97 | #define VIRTIO_CCW_DOING_SET_VQ 0x00010000 |
| 98 | #define VIRTIO_CCW_DOING_RESET 0x00040000 |
| 99 | #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000 |
| 100 | #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000 |
| 101 | #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000 |
| 102 | #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000 |
| 103 | #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000 |
| 104 | #define VIRTIO_CCW_DOING_SET_IND 0x01000000 |
| 105 | #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000 |
| 106 | #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000 |
| 107 | #define VIRTIO_CCW_INTPARM_MASK 0xffff0000 |
| 108 | |
| 109 | static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev) |
| 110 | { |
| 111 | return container_of(vdev, struct virtio_ccw_device, vdev); |
| 112 | } |
| 113 | |
| 114 | static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag) |
| 115 | { |
| 116 | unsigned long flags; |
| 117 | __u32 ret; |
| 118 | |
| 119 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); |
| 120 | if (vcdev->err) |
| 121 | ret = 0; |
| 122 | else |
| 123 | ret = vcdev->curr_io & flag; |
| 124 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); |
| 125 | return ret; |
| 126 | } |
| 127 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 128 | static int ccw_io_helper(struct virtio_ccw_device *vcdev, |
| 129 | struct ccw1 *ccw, __u32 intparm) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 130 | { |
| 131 | int ret; |
| 132 | unsigned long flags; |
| 133 | int flag = intparm & VIRTIO_CCW_INTPARM_MASK; |
| 134 | |
Christian Borntraeger | b26ba22 | 2013-01-07 15:51:52 +0100 | [diff] [blame^] | 135 | do { |
| 136 | spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); |
| 137 | ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0); |
| 138 | if (!ret) |
| 139 | vcdev->curr_io |= flag; |
| 140 | spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); |
| 141 | cpu_relax(); |
| 142 | } while (ret == -EBUSY); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 143 | wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0); |
| 144 | return ret ? ret : vcdev->err; |
| 145 | } |
| 146 | |
| 147 | static inline long do_kvm_notify(struct subchannel_id schid, |
| 148 | unsigned long queue_index) |
| 149 | { |
| 150 | register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY; |
| 151 | register struct subchannel_id __schid asm("2") = schid; |
| 152 | register unsigned long __index asm("3") = queue_index; |
| 153 | register long __rc asm("2"); |
| 154 | |
| 155 | asm volatile ("diag 2,4,0x500\n" |
| 156 | : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index) |
| 157 | : "memory", "cc"); |
| 158 | return __rc; |
| 159 | } |
| 160 | |
| 161 | static void virtio_ccw_kvm_notify(struct virtqueue *vq) |
| 162 | { |
| 163 | struct virtio_ccw_vq_info *info = vq->priv; |
| 164 | struct virtio_ccw_device *vcdev; |
| 165 | struct subchannel_id schid; |
| 166 | |
| 167 | vcdev = to_vc_device(info->vq->vdev); |
| 168 | ccw_device_get_schid(vcdev->cdev, &schid); |
| 169 | do_kvm_notify(schid, virtqueue_get_queue_index(vq)); |
| 170 | } |
| 171 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 172 | static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev, |
| 173 | struct ccw1 *ccw, int index) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 174 | { |
| 175 | vcdev->config_block->index = index; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 176 | ccw->cmd_code = CCW_CMD_READ_VQ_CONF; |
| 177 | ccw->flags = 0; |
| 178 | ccw->count = sizeof(struct vq_config_block); |
| 179 | ccw->cda = (__u32)(unsigned long)(vcdev->config_block); |
| 180 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 181 | return vcdev->config_block->num; |
| 182 | } |
| 183 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 184 | static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 185 | { |
| 186 | struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev); |
| 187 | struct virtio_ccw_vq_info *info = vq->priv; |
| 188 | unsigned long flags; |
| 189 | unsigned long size; |
| 190 | int ret; |
| 191 | unsigned int index = virtqueue_get_queue_index(vq); |
| 192 | |
| 193 | /* Remove from our list. */ |
| 194 | spin_lock_irqsave(&vcdev->lock, flags); |
| 195 | list_del(&info->node); |
| 196 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 197 | |
| 198 | /* Release from host. */ |
| 199 | info->info_block->queue = 0; |
| 200 | info->info_block->align = 0; |
| 201 | info->info_block->index = index; |
| 202 | info->info_block->num = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 203 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 204 | ccw->flags = 0; |
| 205 | ccw->count = sizeof(*info->info_block); |
| 206 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 207 | ret = ccw_io_helper(vcdev, ccw, |
| 208 | VIRTIO_CCW_DOING_SET_VQ | index); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 209 | /* |
| 210 | * -ENODEV isn't considered an error: The device is gone anyway. |
| 211 | * This may happen on device detach. |
| 212 | */ |
| 213 | if (ret && (ret != -ENODEV)) |
| 214 | dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d", |
| 215 | ret, index); |
| 216 | |
| 217 | vring_del_virtqueue(vq); |
| 218 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 219 | free_pages_exact(info->queue, size); |
| 220 | kfree(info->info_block); |
| 221 | kfree(info); |
| 222 | } |
| 223 | |
| 224 | static void virtio_ccw_del_vqs(struct virtio_device *vdev) |
| 225 | { |
| 226 | struct virtqueue *vq, *n; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 227 | struct ccw1 *ccw; |
| 228 | |
| 229 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 230 | if (!ccw) |
| 231 | return; |
| 232 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 233 | |
| 234 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 235 | virtio_ccw_del_vq(vq, ccw); |
| 236 | |
| 237 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev, |
| 241 | int i, vq_callback_t *callback, |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 242 | const char *name, |
| 243 | struct ccw1 *ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 244 | { |
| 245 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 246 | int err; |
| 247 | struct virtqueue *vq; |
| 248 | struct virtio_ccw_vq_info *info; |
| 249 | unsigned long size; |
| 250 | unsigned long flags; |
| 251 | |
| 252 | /* Allocate queue. */ |
| 253 | info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL); |
| 254 | if (!info) { |
| 255 | dev_warn(&vcdev->cdev->dev, "no info\n"); |
| 256 | err = -ENOMEM; |
| 257 | goto out_err; |
| 258 | } |
| 259 | info->info_block = kzalloc(sizeof(*info->info_block), |
| 260 | GFP_DMA | GFP_KERNEL); |
| 261 | if (!info->info_block) { |
| 262 | dev_warn(&vcdev->cdev->dev, "no info block\n"); |
| 263 | err = -ENOMEM; |
| 264 | goto out_err; |
| 265 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 266 | info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 267 | size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN)); |
| 268 | info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| 269 | if (info->queue == NULL) { |
| 270 | dev_warn(&vcdev->cdev->dev, "no queue\n"); |
| 271 | err = -ENOMEM; |
| 272 | goto out_err; |
| 273 | } |
| 274 | |
| 275 | vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev, |
| 276 | true, info->queue, virtio_ccw_kvm_notify, |
| 277 | callback, name); |
| 278 | if (!vq) { |
| 279 | /* For now, we fail if we can't get the requested size. */ |
| 280 | dev_warn(&vcdev->cdev->dev, "no vq\n"); |
| 281 | err = -ENOMEM; |
| 282 | free_pages_exact(info->queue, size); |
| 283 | goto out_err; |
| 284 | } |
| 285 | info->vq = vq; |
| 286 | vq->priv = info; |
| 287 | |
| 288 | /* Register it with the host. */ |
| 289 | info->info_block->queue = (__u64)info->queue; |
| 290 | info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN; |
| 291 | info->info_block->index = i; |
| 292 | info->info_block->num = info->num; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 293 | ccw->cmd_code = CCW_CMD_SET_VQ; |
| 294 | ccw->flags = 0; |
| 295 | ccw->count = sizeof(*info->info_block); |
| 296 | ccw->cda = (__u32)(unsigned long)(info->info_block); |
| 297 | err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 298 | if (err) { |
| 299 | dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n"); |
| 300 | free_pages_exact(info->queue, size); |
| 301 | info->vq = NULL; |
| 302 | vq->priv = NULL; |
| 303 | goto out_err; |
| 304 | } |
| 305 | |
| 306 | /* Save it to our list. */ |
| 307 | spin_lock_irqsave(&vcdev->lock, flags); |
| 308 | list_add(&info->node, &vcdev->virtqueues); |
| 309 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 310 | |
| 311 | return vq; |
| 312 | |
| 313 | out_err: |
| 314 | if (info) |
| 315 | kfree(info->info_block); |
| 316 | kfree(info); |
| 317 | return ERR_PTR(err); |
| 318 | } |
| 319 | |
| 320 | static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 321 | struct virtqueue *vqs[], |
| 322 | vq_callback_t *callbacks[], |
| 323 | const char *names[]) |
| 324 | { |
| 325 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 326 | unsigned long *indicatorp = NULL; |
| 327 | int ret, i; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 328 | struct ccw1 *ccw; |
| 329 | |
| 330 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 331 | if (!ccw) |
| 332 | return -ENOMEM; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 333 | |
| 334 | for (i = 0; i < nvqs; ++i) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 335 | vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i], |
| 336 | ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 337 | if (IS_ERR(vqs[i])) { |
| 338 | ret = PTR_ERR(vqs[i]); |
| 339 | vqs[i] = NULL; |
| 340 | goto out; |
| 341 | } |
| 342 | } |
| 343 | ret = -ENOMEM; |
| 344 | /* We need a data area under 2G to communicate. */ |
| 345 | indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL); |
| 346 | if (!indicatorp) |
| 347 | goto out; |
| 348 | *indicatorp = (unsigned long) &vcdev->indicators; |
| 349 | /* Register queue indicators with host. */ |
| 350 | vcdev->indicators = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 351 | ccw->cmd_code = CCW_CMD_SET_IND; |
| 352 | ccw->flags = 0; |
| 353 | ccw->count = sizeof(vcdev->indicators); |
| 354 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 355 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 356 | if (ret) |
| 357 | goto out; |
| 358 | /* Register indicators2 with host for config changes */ |
| 359 | *indicatorp = (unsigned long) &vcdev->indicators2; |
| 360 | vcdev->indicators2 = 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 361 | ccw->cmd_code = CCW_CMD_SET_CONF_IND; |
| 362 | ccw->flags = 0; |
| 363 | ccw->count = sizeof(vcdev->indicators2); |
| 364 | ccw->cda = (__u32)(unsigned long) indicatorp; |
| 365 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 366 | if (ret) |
| 367 | goto out; |
| 368 | |
| 369 | kfree(indicatorp); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 370 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 371 | return 0; |
| 372 | out: |
| 373 | kfree(indicatorp); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 374 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 375 | virtio_ccw_del_vqs(vdev); |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static void virtio_ccw_reset(struct virtio_device *vdev) |
| 380 | { |
| 381 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 382 | struct ccw1 *ccw; |
| 383 | |
| 384 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 385 | if (!ccw) |
| 386 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 387 | |
| 388 | /* Zero status bits. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 389 | *vcdev->status = 0; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 390 | |
| 391 | /* Send a reset ccw on device. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 392 | ccw->cmd_code = CCW_CMD_VDEV_RESET; |
| 393 | ccw->flags = 0; |
| 394 | ccw->count = 0; |
| 395 | ccw->cda = 0; |
| 396 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET); |
| 397 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static u32 virtio_ccw_get_features(struct virtio_device *vdev) |
| 401 | { |
| 402 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 403 | struct virtio_feature_desc *features; |
| 404 | int ret, rc; |
| 405 | struct ccw1 *ccw; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 406 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 407 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 408 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 409 | return 0; |
| 410 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 411 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
| 412 | if (!features) { |
| 413 | rc = 0; |
| 414 | goto out_free; |
| 415 | } |
| 416 | /* Read the feature bits from the host. */ |
| 417 | /* TODO: Features > 32 bits */ |
| 418 | features->index = 0; |
| 419 | ccw->cmd_code = CCW_CMD_READ_FEAT; |
| 420 | ccw->flags = 0; |
| 421 | ccw->count = sizeof(*features); |
| 422 | ccw->cda = (__u32)(unsigned long)features; |
| 423 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT); |
| 424 | if (ret) { |
| 425 | rc = 0; |
| 426 | goto out_free; |
| 427 | } |
| 428 | |
| 429 | rc = le32_to_cpu(features->features); |
| 430 | |
| 431 | out_free: |
| 432 | kfree(features); |
| 433 | kfree(ccw); |
| 434 | return rc; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static void virtio_ccw_finalize_features(struct virtio_device *vdev) |
| 438 | { |
| 439 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 440 | struct virtio_feature_desc *features; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 441 | int i; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 442 | struct ccw1 *ccw; |
| 443 | |
| 444 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 445 | if (!ccw) |
| 446 | return; |
| 447 | |
| 448 | features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL); |
| 449 | if (!features) |
| 450 | goto out_free; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 451 | |
| 452 | /* Give virtio_ring a chance to accept features. */ |
| 453 | vring_transport_features(vdev); |
| 454 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 455 | for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 456 | i++) { |
| 457 | int highbits = i % 2 ? 32 : 0; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 458 | features->index = i; |
| 459 | features->features = cpu_to_le32(vdev->features[i / 2] |
| 460 | >> highbits); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 461 | /* Write the feature bits to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 462 | ccw->cmd_code = CCW_CMD_WRITE_FEAT; |
| 463 | ccw->flags = 0; |
| 464 | ccw->count = sizeof(*features); |
| 465 | ccw->cda = (__u32)(unsigned long)features; |
| 466 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 467 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 468 | out_free: |
| 469 | kfree(features); |
| 470 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void virtio_ccw_get_config(struct virtio_device *vdev, |
| 474 | unsigned int offset, void *buf, unsigned len) |
| 475 | { |
| 476 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 477 | int ret; |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 478 | struct ccw1 *ccw; |
| 479 | void *config_area; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 480 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 481 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 482 | if (!ccw) |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 483 | return; |
| 484 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 485 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 486 | if (!config_area) |
| 487 | goto out_free; |
| 488 | |
| 489 | /* Read the config area from the host. */ |
| 490 | ccw->cmd_code = CCW_CMD_READ_CONF; |
| 491 | ccw->flags = 0; |
| 492 | ccw->count = offset + len; |
| 493 | ccw->cda = (__u32)(unsigned long)config_area; |
| 494 | ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG); |
| 495 | if (ret) |
| 496 | goto out_free; |
| 497 | |
| 498 | memcpy(vcdev->config, config_area, sizeof(vcdev->config)); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 499 | memcpy(buf, &vcdev->config[offset], len); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 500 | |
| 501 | out_free: |
| 502 | kfree(config_area); |
| 503 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static void virtio_ccw_set_config(struct virtio_device *vdev, |
| 507 | unsigned int offset, const void *buf, |
| 508 | unsigned len) |
| 509 | { |
| 510 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 511 | struct ccw1 *ccw; |
| 512 | void *config_area; |
| 513 | |
| 514 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 515 | if (!ccw) |
| 516 | return; |
| 517 | |
| 518 | config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL); |
| 519 | if (!config_area) |
| 520 | goto out_free; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 521 | |
| 522 | memcpy(&vcdev->config[offset], buf, len); |
| 523 | /* Write the config area to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 524 | memcpy(config_area, vcdev->config, sizeof(vcdev->config)); |
| 525 | ccw->cmd_code = CCW_CMD_WRITE_CONF; |
| 526 | ccw->flags = 0; |
| 527 | ccw->count = offset + len; |
| 528 | ccw->cda = (__u32)(unsigned long)config_area; |
| 529 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG); |
| 530 | |
| 531 | out_free: |
| 532 | kfree(config_area); |
| 533 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static u8 virtio_ccw_get_status(struct virtio_device *vdev) |
| 537 | { |
| 538 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
| 539 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 540 | return *vcdev->status; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status) |
| 544 | { |
| 545 | struct virtio_ccw_device *vcdev = to_vc_device(vdev); |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 546 | struct ccw1 *ccw; |
| 547 | |
| 548 | ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL); |
| 549 | if (!ccw) |
| 550 | return; |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 551 | |
| 552 | /* Write the status to the host. */ |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 553 | *vcdev->status = status; |
| 554 | ccw->cmd_code = CCW_CMD_WRITE_STATUS; |
| 555 | ccw->flags = 0; |
| 556 | ccw->count = sizeof(status); |
| 557 | ccw->cda = (__u32)(unsigned long)vcdev->status; |
| 558 | ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS); |
| 559 | kfree(ccw); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static struct virtio_config_ops virtio_ccw_config_ops = { |
| 563 | .get_features = virtio_ccw_get_features, |
| 564 | .finalize_features = virtio_ccw_finalize_features, |
| 565 | .get = virtio_ccw_get_config, |
| 566 | .set = virtio_ccw_set_config, |
| 567 | .get_status = virtio_ccw_get_status, |
| 568 | .set_status = virtio_ccw_set_status, |
| 569 | .reset = virtio_ccw_reset, |
| 570 | .find_vqs = virtio_ccw_find_vqs, |
| 571 | .del_vqs = virtio_ccw_del_vqs, |
| 572 | }; |
| 573 | |
| 574 | |
| 575 | /* |
| 576 | * ccw bus driver related functions |
| 577 | */ |
| 578 | |
| 579 | static void virtio_ccw_release_dev(struct device *_d) |
| 580 | { |
| 581 | struct virtio_device *dev = container_of(_d, struct virtio_device, |
| 582 | dev); |
| 583 | struct virtio_ccw_device *vcdev = to_vc_device(dev); |
| 584 | |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 585 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 586 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 587 | kfree(vcdev); |
| 588 | } |
| 589 | |
| 590 | static int irb_is_error(struct irb *irb) |
| 591 | { |
| 592 | if (scsw_cstat(&irb->scsw) != 0) |
| 593 | return 1; |
| 594 | if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) |
| 595 | return 1; |
| 596 | if (scsw_cc(&irb->scsw) != 0) |
| 597 | return 1; |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev, |
| 602 | int index) |
| 603 | { |
| 604 | struct virtio_ccw_vq_info *info; |
| 605 | unsigned long flags; |
| 606 | struct virtqueue *vq; |
| 607 | |
| 608 | vq = NULL; |
| 609 | spin_lock_irqsave(&vcdev->lock, flags); |
| 610 | list_for_each_entry(info, &vcdev->virtqueues, node) { |
| 611 | if (virtqueue_get_queue_index(info->vq) == index) { |
| 612 | vq = info->vq; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | spin_unlock_irqrestore(&vcdev->lock, flags); |
| 617 | return vq; |
| 618 | } |
| 619 | |
| 620 | static void virtio_ccw_int_handler(struct ccw_device *cdev, |
| 621 | unsigned long intparm, |
| 622 | struct irb *irb) |
| 623 | { |
| 624 | __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK; |
| 625 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 626 | int i; |
| 627 | struct virtqueue *vq; |
| 628 | struct virtio_driver *drv; |
| 629 | |
| 630 | /* Check if it's a notification from the host. */ |
| 631 | if ((intparm == 0) && |
| 632 | (scsw_stctl(&irb->scsw) == |
| 633 | (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) { |
| 634 | /* OK */ |
| 635 | } |
| 636 | if (irb_is_error(irb)) |
| 637 | vcdev->err = -EIO; /* XXX - use real error */ |
| 638 | if (vcdev->curr_io & activity) { |
| 639 | switch (activity) { |
| 640 | case VIRTIO_CCW_DOING_READ_FEAT: |
| 641 | case VIRTIO_CCW_DOING_WRITE_FEAT: |
| 642 | case VIRTIO_CCW_DOING_READ_CONFIG: |
| 643 | case VIRTIO_CCW_DOING_WRITE_CONFIG: |
| 644 | case VIRTIO_CCW_DOING_WRITE_STATUS: |
| 645 | case VIRTIO_CCW_DOING_SET_VQ: |
| 646 | case VIRTIO_CCW_DOING_SET_IND: |
| 647 | case VIRTIO_CCW_DOING_SET_CONF_IND: |
| 648 | case VIRTIO_CCW_DOING_RESET: |
| 649 | case VIRTIO_CCW_DOING_READ_VQ_CONF: |
| 650 | vcdev->curr_io &= ~activity; |
| 651 | wake_up(&vcdev->wait_q); |
| 652 | break; |
| 653 | default: |
| 654 | /* don't know what to do... */ |
| 655 | dev_warn(&cdev->dev, "Suspicious activity '%08x'\n", |
| 656 | activity); |
| 657 | WARN_ON(1); |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | for_each_set_bit(i, &vcdev->indicators, |
| 662 | sizeof(vcdev->indicators) * BITS_PER_BYTE) { |
| 663 | /* The bit clear must happen before the vring kick. */ |
| 664 | clear_bit(i, &vcdev->indicators); |
| 665 | barrier(); |
| 666 | vq = virtio_ccw_vq_by_ind(vcdev, i); |
| 667 | vring_interrupt(0, vq); |
| 668 | } |
| 669 | if (test_bit(0, &vcdev->indicators2)) { |
| 670 | drv = container_of(vcdev->vdev.dev.driver, |
| 671 | struct virtio_driver, driver); |
| 672 | |
| 673 | if (drv && drv->config_changed) |
| 674 | drv->config_changed(&vcdev->vdev); |
| 675 | clear_bit(0, &vcdev->indicators2); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * We usually want to autoonline all devices, but give the admin |
| 681 | * a way to exempt devices from this. |
| 682 | */ |
| 683 | #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \ |
| 684 | (8*sizeof(long))) |
| 685 | static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS]; |
| 686 | |
| 687 | static char *no_auto = ""; |
| 688 | |
| 689 | module_param(no_auto, charp, 0444); |
| 690 | MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined"); |
| 691 | |
| 692 | static int virtio_ccw_check_autoonline(struct ccw_device *cdev) |
| 693 | { |
| 694 | struct ccw_dev_id id; |
| 695 | |
| 696 | ccw_device_get_id(cdev, &id); |
| 697 | if (test_bit(id.devno, devs_no_auto[id.ssid])) |
| 698 | return 0; |
| 699 | return 1; |
| 700 | } |
| 701 | |
| 702 | static void virtio_ccw_auto_online(void *data, async_cookie_t cookie) |
| 703 | { |
| 704 | struct ccw_device *cdev = data; |
| 705 | int ret; |
| 706 | |
| 707 | ret = ccw_device_set_online(cdev); |
| 708 | if (ret) |
| 709 | dev_warn(&cdev->dev, "Failed to set online: %d\n", ret); |
| 710 | } |
| 711 | |
| 712 | static int virtio_ccw_probe(struct ccw_device *cdev) |
| 713 | { |
| 714 | cdev->handler = virtio_ccw_int_handler; |
| 715 | |
| 716 | if (virtio_ccw_check_autoonline(cdev)) |
| 717 | async_schedule(virtio_ccw_auto_online, cdev); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static void virtio_ccw_remove(struct ccw_device *cdev) |
| 722 | { |
| 723 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 724 | |
| 725 | if (cdev->online) { |
| 726 | unregister_virtio_device(&vcdev->vdev); |
| 727 | dev_set_drvdata(&cdev->dev, NULL); |
| 728 | } |
| 729 | cdev->handler = NULL; |
| 730 | } |
| 731 | |
| 732 | static int virtio_ccw_offline(struct ccw_device *cdev) |
| 733 | { |
| 734 | struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev); |
| 735 | |
| 736 | unregister_virtio_device(&vcdev->vdev); |
| 737 | dev_set_drvdata(&cdev->dev, NULL); |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 742 | static int virtio_ccw_online(struct ccw_device *cdev) |
| 743 | { |
| 744 | int ret; |
| 745 | struct virtio_ccw_device *vcdev; |
| 746 | |
| 747 | vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL); |
| 748 | if (!vcdev) { |
| 749 | dev_warn(&cdev->dev, "Could not get memory for virtio\n"); |
| 750 | ret = -ENOMEM; |
| 751 | goto out_free; |
| 752 | } |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 753 | vcdev->config_block = kzalloc(sizeof(*vcdev->config_block), |
| 754 | GFP_DMA | GFP_KERNEL); |
| 755 | if (!vcdev->config_block) { |
| 756 | ret = -ENOMEM; |
| 757 | goto out_free; |
| 758 | } |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 759 | vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL); |
| 760 | if (!vcdev->status) { |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 761 | ret = -ENOMEM; |
| 762 | goto out_free; |
| 763 | } |
| 764 | |
| 765 | vcdev->vdev.dev.parent = &cdev->dev; |
| 766 | vcdev->vdev.dev.release = virtio_ccw_release_dev; |
| 767 | vcdev->vdev.config = &virtio_ccw_config_ops; |
| 768 | vcdev->cdev = cdev; |
| 769 | init_waitqueue_head(&vcdev->wait_q); |
| 770 | INIT_LIST_HEAD(&vcdev->virtqueues); |
| 771 | spin_lock_init(&vcdev->lock); |
| 772 | |
| 773 | dev_set_drvdata(&cdev->dev, vcdev); |
| 774 | vcdev->vdev.id.vendor = cdev->id.cu_type; |
| 775 | vcdev->vdev.id.device = cdev->id.cu_model; |
| 776 | ret = register_virtio_device(&vcdev->vdev); |
| 777 | if (ret) { |
| 778 | dev_warn(&cdev->dev, "Failed to register virtio device: %d\n", |
| 779 | ret); |
| 780 | goto out_put; |
| 781 | } |
| 782 | return 0; |
| 783 | out_put: |
| 784 | dev_set_drvdata(&cdev->dev, NULL); |
| 785 | put_device(&vcdev->vdev.dev); |
| 786 | return ret; |
| 787 | out_free: |
| 788 | if (vcdev) { |
Cornelia Huck | 73fa21e | 2013-01-07 15:51:51 +0100 | [diff] [blame] | 789 | kfree(vcdev->status); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 790 | kfree(vcdev->config_block); |
Cornelia Huck | 7e64e05 | 2012-12-14 17:02:18 +0100 | [diff] [blame] | 791 | } |
| 792 | kfree(vcdev); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event) |
| 797 | { |
| 798 | /* TODO: Check whether we need special handling here. */ |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static struct ccw_device_id virtio_ids[] = { |
| 803 | { CCW_DEVICE(0x3832, 0) }, |
| 804 | {}, |
| 805 | }; |
| 806 | MODULE_DEVICE_TABLE(ccw, virtio_ids); |
| 807 | |
| 808 | static struct ccw_driver virtio_ccw_driver = { |
| 809 | .driver = { |
| 810 | .owner = THIS_MODULE, |
| 811 | .name = "virtio_ccw", |
| 812 | }, |
| 813 | .ids = virtio_ids, |
| 814 | .probe = virtio_ccw_probe, |
| 815 | .remove = virtio_ccw_remove, |
| 816 | .set_offline = virtio_ccw_offline, |
| 817 | .set_online = virtio_ccw_online, |
| 818 | .notify = virtio_ccw_cio_notify, |
| 819 | .int_class = IOINT_VIR, |
| 820 | }; |
| 821 | |
| 822 | static int __init pure_hex(char **cp, unsigned int *val, int min_digit, |
| 823 | int max_digit, int max_val) |
| 824 | { |
| 825 | int diff; |
| 826 | |
| 827 | diff = 0; |
| 828 | *val = 0; |
| 829 | |
| 830 | while (diff <= max_digit) { |
| 831 | int value = hex_to_bin(**cp); |
| 832 | |
| 833 | if (value < 0) |
| 834 | break; |
| 835 | *val = *val * 16 + value; |
| 836 | (*cp)++; |
| 837 | diff++; |
| 838 | } |
| 839 | |
| 840 | if ((diff < min_digit) || (diff > max_digit) || (*val > max_val)) |
| 841 | return 1; |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int __init parse_busid(char *str, unsigned int *cssid, |
| 847 | unsigned int *ssid, unsigned int *devno) |
| 848 | { |
| 849 | char *str_work; |
| 850 | int rc, ret; |
| 851 | |
| 852 | rc = 1; |
| 853 | |
| 854 | if (*str == '\0') |
| 855 | goto out; |
| 856 | |
| 857 | str_work = str; |
| 858 | ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID); |
| 859 | if (ret || (str_work[0] != '.')) |
| 860 | goto out; |
| 861 | str_work++; |
| 862 | ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID); |
| 863 | if (ret || (str_work[0] != '.')) |
| 864 | goto out; |
| 865 | str_work++; |
| 866 | ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL); |
| 867 | if (ret || (str_work[0] != '\0')) |
| 868 | goto out; |
| 869 | |
| 870 | rc = 0; |
| 871 | out: |
| 872 | return rc; |
| 873 | } |
| 874 | |
| 875 | static void __init no_auto_parse(void) |
| 876 | { |
| 877 | unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to; |
| 878 | char *parm, *str; |
| 879 | int rc; |
| 880 | |
| 881 | str = no_auto; |
| 882 | while ((parm = strsep(&str, ","))) { |
| 883 | rc = parse_busid(strsep(&parm, "-"), &from_cssid, |
| 884 | &from_ssid, &from); |
| 885 | if (rc) |
| 886 | continue; |
| 887 | if (parm != NULL) { |
| 888 | rc = parse_busid(parm, &to_cssid, |
| 889 | &to_ssid, &to); |
| 890 | if ((from_ssid > to_ssid) || |
| 891 | ((from_ssid == to_ssid) && (from > to))) |
| 892 | rc = -EINVAL; |
| 893 | } else { |
| 894 | to_cssid = from_cssid; |
| 895 | to_ssid = from_ssid; |
| 896 | to = from; |
| 897 | } |
| 898 | if (rc) |
| 899 | continue; |
| 900 | while ((from_ssid < to_ssid) || |
| 901 | ((from_ssid == to_ssid) && (from <= to))) { |
| 902 | set_bit(from, devs_no_auto[from_ssid]); |
| 903 | from++; |
| 904 | if (from > __MAX_SUBCHANNEL) { |
| 905 | from_ssid++; |
| 906 | from = 0; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | static int __init virtio_ccw_init(void) |
| 913 | { |
| 914 | /* parse no_auto string before we do anything further */ |
| 915 | no_auto_parse(); |
| 916 | return ccw_driver_register(&virtio_ccw_driver); |
| 917 | } |
| 918 | module_init(virtio_ccw_init); |
| 919 | |
| 920 | static void __exit virtio_ccw_exit(void) |
| 921 | { |
| 922 | ccw_driver_unregister(&virtio_ccw_driver); |
| 923 | } |
| 924 | module_exit(virtio_ccw_exit); |