s390/kvm: Fix store status for ACRS/FPRS
[linux-block.git] / drivers / s390 / kvm / virtio_ccw.c
CommitLineData
7e64e059
CH
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
39struct 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
47struct virtio_ccw_device {
48 struct virtio_device vdev;
73fa21ea 49 __u8 *status;
7e64e059
CH
50 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
51 struct ccw_device *cdev;
7e64e059
CH
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
62struct vq_info_block {
63 __u64 queue;
64 __u32 align;
65 __u16 index;
66 __u16 num;
67} __packed;
68
69struct virtio_feature_desc {
70 __u32 features;
71 __u8 index;
72} __packed;
73
74struct 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
109static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
110{
111 return container_of(vdev, struct virtio_ccw_device, vdev);
112}
113
114static 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
73fa21ea
CH
128static int ccw_io_helper(struct virtio_ccw_device *vcdev,
129 struct ccw1 *ccw, __u32 intparm)
7e64e059
CH
130{
131 int ret;
132 unsigned long flags;
133 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
134
b26ba22b
CB
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);
7e64e059
CH
143 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
144 return ret ? ret : vcdev->err;
145}
146
147static 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
161static 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
73fa21ea
CH
172static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
173 struct ccw1 *ccw, int index)
7e64e059
CH
174{
175 vcdev->config_block->index = index;
73fa21ea
CH
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);
7e64e059
CH
181 return vcdev->config_block->num;
182}
183
73fa21ea 184static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
7e64e059
CH
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;
73fa21ea
CH
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);
7e64e059
CH
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
224static void virtio_ccw_del_vqs(struct virtio_device *vdev)
225{
226 struct virtqueue *vq, *n;
73fa21ea
CH
227 struct ccw1 *ccw;
228
229 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
230 if (!ccw)
231 return;
232
7e64e059
CH
233
234 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
73fa21ea
CH
235 virtio_ccw_del_vq(vq, ccw);
236
237 kfree(ccw);
7e64e059
CH
238}
239
240static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
241 int i, vq_callback_t *callback,
73fa21ea
CH
242 const char *name,
243 struct ccw1 *ccw)
7e64e059
CH
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 }
73fa21ea 266 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
7e64e059
CH
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;
73fa21ea
CH
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);
7e64e059
CH
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
313out_err:
314 if (info)
315 kfree(info->info_block);
316 kfree(info);
317 return ERR_PTR(err);
318}
319
320static 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;
73fa21ea
CH
328 struct ccw1 *ccw;
329
330 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
331 if (!ccw)
332 return -ENOMEM;
7e64e059
CH
333
334 for (i = 0; i < nvqs; ++i) {
73fa21ea
CH
335 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
336 ccw);
7e64e059
CH
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;
73fa21ea
CH
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);
7e64e059
CH
356 if (ret)
357 goto out;
358 /* Register indicators2 with host for config changes */
359 *indicatorp = (unsigned long) &vcdev->indicators2;
360 vcdev->indicators2 = 0;
73fa21ea
CH
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);
7e64e059
CH
366 if (ret)
367 goto out;
368
369 kfree(indicatorp);
73fa21ea 370 kfree(ccw);
7e64e059
CH
371 return 0;
372out:
373 kfree(indicatorp);
73fa21ea 374 kfree(ccw);
7e64e059
CH
375 virtio_ccw_del_vqs(vdev);
376 return ret;
377}
378
379static void virtio_ccw_reset(struct virtio_device *vdev)
380{
381 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
382 struct ccw1 *ccw;
383
384 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
385 if (!ccw)
386 return;
7e64e059
CH
387
388 /* Zero status bits. */
73fa21ea 389 *vcdev->status = 0;
7e64e059
CH
390
391 /* Send a reset ccw on device. */
73fa21ea
CH
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);
7e64e059
CH
398}
399
400static u32 virtio_ccw_get_features(struct virtio_device *vdev)
401{
402 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
403 struct virtio_feature_desc *features;
404 int ret, rc;
405 struct ccw1 *ccw;
7e64e059 406
73fa21ea
CH
407 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
408 if (!ccw)
409 return 0;
410
411 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
412 if (!features) {
413 rc = 0;
414 goto out_free;
415 }
7e64e059
CH
416 /* Read the feature bits from the host. */
417 /* TODO: Features > 32 bits */
73fa21ea
CH
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);
7e64e059 430
73fa21ea
CH
431out_free:
432 kfree(features);
433 kfree(ccw);
434 return rc;
7e64e059
CH
435}
436
437static void virtio_ccw_finalize_features(struct virtio_device *vdev)
438{
439 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea 440 struct virtio_feature_desc *features;
7e64e059 441 int i;
73fa21ea
CH
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;
7e64e059
CH
451
452 /* Give virtio_ring a chance to accept features. */
453 vring_transport_features(vdev);
454
73fa21ea 455 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
7e64e059
CH
456 i++) {
457 int highbits = i % 2 ? 32 : 0;
73fa21ea
CH
458 features->index = i;
459 features->features = cpu_to_le32(vdev->features[i / 2]
460 >> highbits);
7e64e059 461 /* Write the feature bits to the host. */
73fa21ea
CH
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);
7e64e059 467 }
73fa21ea
CH
468out_free:
469 kfree(features);
470 kfree(ccw);
7e64e059
CH
471}
472
473static 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;
73fa21ea
CH
478 struct ccw1 *ccw;
479 void *config_area;
480
481 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
482 if (!ccw)
483 return;
484
485 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
486 if (!config_area)
487 goto out_free;
7e64e059
CH
488
489 /* Read the config area from the host. */
73fa21ea
CH
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);
7e64e059 495 if (ret)
73fa21ea 496 goto out_free;
7e64e059 497
73fa21ea 498 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
7e64e059 499 memcpy(buf, &vcdev->config[offset], len);
73fa21ea
CH
500
501out_free:
502 kfree(config_area);
503 kfree(ccw);
7e64e059
CH
504}
505
506static 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);
73fa21ea
CH
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;
7e64e059
CH
521
522 memcpy(&vcdev->config[offset], buf, len);
523 /* Write the config area to the host. */
73fa21ea
CH
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
531out_free:
532 kfree(config_area);
533 kfree(ccw);
7e64e059
CH
534}
535
536static u8 virtio_ccw_get_status(struct virtio_device *vdev)
537{
538 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
539
73fa21ea 540 return *vcdev->status;
7e64e059
CH
541}
542
543static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
544{
545 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
546 struct ccw1 *ccw;
547
548 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
549 if (!ccw)
550 return;
7e64e059
CH
551
552 /* Write the status to the host. */
73fa21ea
CH
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);
7e64e059
CH
560}
561
562static 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
579static 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
73fa21ea 585 kfree(vcdev->status);
7e64e059 586 kfree(vcdev->config_block);
7e64e059
CH
587 kfree(vcdev);
588}
589
590static 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
601static 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
620static 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)))
685static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
686
687static char *no_auto = "";
688
689module_param(no_auto, charp, 0444);
690MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
691
692static 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
702static 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
712static 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
721static 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
732static 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
7e64e059
CH
742static 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 }
7e64e059
CH
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 }
73fa21ea
CH
759 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
760 if (!vcdev->status) {
7e64e059
CH
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;
783out_put:
784 dev_set_drvdata(&cdev->dev, NULL);
785 put_device(&vcdev->vdev.dev);
786 return ret;
787out_free:
788 if (vcdev) {
73fa21ea 789 kfree(vcdev->status);
7e64e059 790 kfree(vcdev->config_block);
7e64e059
CH
791 }
792 kfree(vcdev);
793 return ret;
794}
795
796static 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
802static struct ccw_device_id virtio_ids[] = {
803 { CCW_DEVICE(0x3832, 0) },
804 {},
805};
806MODULE_DEVICE_TABLE(ccw, virtio_ids);
807
808static 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
822static 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
846static 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;
871out:
872 return rc;
873}
874
875static 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
912static 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}
918module_init(virtio_ccw_init);
919
920static void __exit virtio_ccw_exit(void)
921{
922 ccw_driver_unregister(&virtio_ccw_driver);
923}
924module_exit(virtio_ccw_exit);