blob: 70419a75d0e099ac03b1ef34f837803fcc5f4395 [file] [log] [blame]
Cornelia Huck7e64e052012-12-14 17:02:18 +01001/*
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;
Cornelia Huck73fa21e2013-01-07 15:51:51 +010049 __u8 *status;
Cornelia Huck7e64e052012-12-14 17:02:18 +010050 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
51 struct ccw_device *cdev;
Cornelia Huck7e64e052012-12-14 17:02:18 +010052 __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
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100128static int ccw_io_helper(struct virtio_ccw_device *vcdev,
129 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100130{
131 int ret;
132 unsigned long flags;
133 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
134
135 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100136 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100137 if (!ret)
138 vcdev->curr_io |= flag;
139 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
140 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
141 return ret ? ret : vcdev->err;
142}
143
144static inline long do_kvm_notify(struct subchannel_id schid,
145 unsigned long queue_index)
146{
147 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
148 register struct subchannel_id __schid asm("2") = schid;
149 register unsigned long __index asm("3") = queue_index;
150 register long __rc asm("2");
151
152 asm volatile ("diag 2,4,0x500\n"
153 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index)
154 : "memory", "cc");
155 return __rc;
156}
157
158static void virtio_ccw_kvm_notify(struct virtqueue *vq)
159{
160 struct virtio_ccw_vq_info *info = vq->priv;
161 struct virtio_ccw_device *vcdev;
162 struct subchannel_id schid;
163
164 vcdev = to_vc_device(info->vq->vdev);
165 ccw_device_get_schid(vcdev->cdev, &schid);
166 do_kvm_notify(schid, virtqueue_get_queue_index(vq));
167}
168
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100169static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
170 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100171{
172 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100173 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
174 ccw->flags = 0;
175 ccw->count = sizeof(struct vq_config_block);
176 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
177 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100178 return vcdev->config_block->num;
179}
180
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100181static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100182{
183 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
184 struct virtio_ccw_vq_info *info = vq->priv;
185 unsigned long flags;
186 unsigned long size;
187 int ret;
188 unsigned int index = virtqueue_get_queue_index(vq);
189
190 /* Remove from our list. */
191 spin_lock_irqsave(&vcdev->lock, flags);
192 list_del(&info->node);
193 spin_unlock_irqrestore(&vcdev->lock, flags);
194
195 /* Release from host. */
196 info->info_block->queue = 0;
197 info->info_block->align = 0;
198 info->info_block->index = index;
199 info->info_block->num = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100200 ccw->cmd_code = CCW_CMD_SET_VQ;
201 ccw->flags = 0;
202 ccw->count = sizeof(*info->info_block);
203 ccw->cda = (__u32)(unsigned long)(info->info_block);
204 ret = ccw_io_helper(vcdev, ccw,
205 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100206 /*
207 * -ENODEV isn't considered an error: The device is gone anyway.
208 * This may happen on device detach.
209 */
210 if (ret && (ret != -ENODEV))
211 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
212 ret, index);
213
214 vring_del_virtqueue(vq);
215 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
216 free_pages_exact(info->queue, size);
217 kfree(info->info_block);
218 kfree(info);
219}
220
221static void virtio_ccw_del_vqs(struct virtio_device *vdev)
222{
223 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100224 struct ccw1 *ccw;
225
226 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
227 if (!ccw)
228 return;
229
Cornelia Huck7e64e052012-12-14 17:02:18 +0100230
231 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100232 virtio_ccw_del_vq(vq, ccw);
233
234 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100235}
236
237static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
238 int i, vq_callback_t *callback,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100239 const char *name,
240 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100241{
242 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
243 int err;
244 struct virtqueue *vq;
245 struct virtio_ccw_vq_info *info;
246 unsigned long size;
247 unsigned long flags;
248
249 /* Allocate queue. */
250 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
251 if (!info) {
252 dev_warn(&vcdev->cdev->dev, "no info\n");
253 err = -ENOMEM;
254 goto out_err;
255 }
256 info->info_block = kzalloc(sizeof(*info->info_block),
257 GFP_DMA | GFP_KERNEL);
258 if (!info->info_block) {
259 dev_warn(&vcdev->cdev->dev, "no info block\n");
260 err = -ENOMEM;
261 goto out_err;
262 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100263 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100264 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
265 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
266 if (info->queue == NULL) {
267 dev_warn(&vcdev->cdev->dev, "no queue\n");
268 err = -ENOMEM;
269 goto out_err;
270 }
271
272 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
273 true, info->queue, virtio_ccw_kvm_notify,
274 callback, name);
275 if (!vq) {
276 /* For now, we fail if we can't get the requested size. */
277 dev_warn(&vcdev->cdev->dev, "no vq\n");
278 err = -ENOMEM;
279 free_pages_exact(info->queue, size);
280 goto out_err;
281 }
282 info->vq = vq;
283 vq->priv = info;
284
285 /* Register it with the host. */
286 info->info_block->queue = (__u64)info->queue;
287 info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
288 info->info_block->index = i;
289 info->info_block->num = info->num;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100290 ccw->cmd_code = CCW_CMD_SET_VQ;
291 ccw->flags = 0;
292 ccw->count = sizeof(*info->info_block);
293 ccw->cda = (__u32)(unsigned long)(info->info_block);
294 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100295 if (err) {
296 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
297 free_pages_exact(info->queue, size);
298 info->vq = NULL;
299 vq->priv = NULL;
300 goto out_err;
301 }
302
303 /* Save it to our list. */
304 spin_lock_irqsave(&vcdev->lock, flags);
305 list_add(&info->node, &vcdev->virtqueues);
306 spin_unlock_irqrestore(&vcdev->lock, flags);
307
308 return vq;
309
310out_err:
311 if (info)
312 kfree(info->info_block);
313 kfree(info);
314 return ERR_PTR(err);
315}
316
317static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
318 struct virtqueue *vqs[],
319 vq_callback_t *callbacks[],
320 const char *names[])
321{
322 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
323 unsigned long *indicatorp = NULL;
324 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100325 struct ccw1 *ccw;
326
327 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
328 if (!ccw)
329 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100330
331 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100332 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
333 ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100334 if (IS_ERR(vqs[i])) {
335 ret = PTR_ERR(vqs[i]);
336 vqs[i] = NULL;
337 goto out;
338 }
339 }
340 ret = -ENOMEM;
341 /* We need a data area under 2G to communicate. */
342 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
343 if (!indicatorp)
344 goto out;
345 *indicatorp = (unsigned long) &vcdev->indicators;
346 /* Register queue indicators with host. */
347 vcdev->indicators = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100348 ccw->cmd_code = CCW_CMD_SET_IND;
349 ccw->flags = 0;
350 ccw->count = sizeof(vcdev->indicators);
351 ccw->cda = (__u32)(unsigned long) indicatorp;
352 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100353 if (ret)
354 goto out;
355 /* Register indicators2 with host for config changes */
356 *indicatorp = (unsigned long) &vcdev->indicators2;
357 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100358 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
359 ccw->flags = 0;
360 ccw->count = sizeof(vcdev->indicators2);
361 ccw->cda = (__u32)(unsigned long) indicatorp;
362 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100363 if (ret)
364 goto out;
365
366 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100367 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100368 return 0;
369out:
370 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100371 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100372 virtio_ccw_del_vqs(vdev);
373 return ret;
374}
375
376static void virtio_ccw_reset(struct virtio_device *vdev)
377{
378 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100379 struct ccw1 *ccw;
380
381 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
382 if (!ccw)
383 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100384
385 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100386 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100387
388 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100389 ccw->cmd_code = CCW_CMD_VDEV_RESET;
390 ccw->flags = 0;
391 ccw->count = 0;
392 ccw->cda = 0;
393 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
394 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100395}
396
397static u32 virtio_ccw_get_features(struct virtio_device *vdev)
398{
399 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100400 struct virtio_feature_desc *features;
401 int ret, rc;
402 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100403
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100404 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
405 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100406 return 0;
407
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100408 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
409 if (!features) {
410 rc = 0;
411 goto out_free;
412 }
413 /* Read the feature bits from the host. */
414 /* TODO: Features > 32 bits */
415 features->index = 0;
416 ccw->cmd_code = CCW_CMD_READ_FEAT;
417 ccw->flags = 0;
418 ccw->count = sizeof(*features);
419 ccw->cda = (__u32)(unsigned long)features;
420 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
421 if (ret) {
422 rc = 0;
423 goto out_free;
424 }
425
426 rc = le32_to_cpu(features->features);
427
428out_free:
429 kfree(features);
430 kfree(ccw);
431 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100432}
433
434static void virtio_ccw_finalize_features(struct virtio_device *vdev)
435{
436 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100437 struct virtio_feature_desc *features;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100438 int i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100439 struct ccw1 *ccw;
440
441 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
442 if (!ccw)
443 return;
444
445 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
446 if (!features)
447 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100448
449 /* Give virtio_ring a chance to accept features. */
450 vring_transport_features(vdev);
451
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100452 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100453 i++) {
454 int highbits = i % 2 ? 32 : 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100455 features->index = i;
456 features->features = cpu_to_le32(vdev->features[i / 2]
457 >> highbits);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100458 /* Write the feature bits to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100459 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
460 ccw->flags = 0;
461 ccw->count = sizeof(*features);
462 ccw->cda = (__u32)(unsigned long)features;
463 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100464 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100465out_free:
466 kfree(features);
467 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100468}
469
470static void virtio_ccw_get_config(struct virtio_device *vdev,
471 unsigned int offset, void *buf, unsigned len)
472{
473 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
474 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100475 struct ccw1 *ccw;
476 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100477
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100478 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
479 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100480 return;
481
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100482 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
483 if (!config_area)
484 goto out_free;
485
486 /* Read the config area from the host. */
487 ccw->cmd_code = CCW_CMD_READ_CONF;
488 ccw->flags = 0;
489 ccw->count = offset + len;
490 ccw->cda = (__u32)(unsigned long)config_area;
491 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
492 if (ret)
493 goto out_free;
494
495 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
Cornelia Huck7e64e052012-12-14 17:02:18 +0100496 memcpy(buf, &vcdev->config[offset], len);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100497
498out_free:
499 kfree(config_area);
500 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100501}
502
503static void virtio_ccw_set_config(struct virtio_device *vdev,
504 unsigned int offset, const void *buf,
505 unsigned len)
506{
507 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100508 struct ccw1 *ccw;
509 void *config_area;
510
511 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
512 if (!ccw)
513 return;
514
515 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
516 if (!config_area)
517 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100518
519 memcpy(&vcdev->config[offset], buf, len);
520 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100521 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
522 ccw->cmd_code = CCW_CMD_WRITE_CONF;
523 ccw->flags = 0;
524 ccw->count = offset + len;
525 ccw->cda = (__u32)(unsigned long)config_area;
526 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
527
528out_free:
529 kfree(config_area);
530 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100531}
532
533static u8 virtio_ccw_get_status(struct virtio_device *vdev)
534{
535 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
536
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100537 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100538}
539
540static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
541{
542 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100543 struct ccw1 *ccw;
544
545 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
546 if (!ccw)
547 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100548
549 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100550 *vcdev->status = status;
551 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
552 ccw->flags = 0;
553 ccw->count = sizeof(status);
554 ccw->cda = (__u32)(unsigned long)vcdev->status;
555 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
556 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100557}
558
559static struct virtio_config_ops virtio_ccw_config_ops = {
560 .get_features = virtio_ccw_get_features,
561 .finalize_features = virtio_ccw_finalize_features,
562 .get = virtio_ccw_get_config,
563 .set = virtio_ccw_set_config,
564 .get_status = virtio_ccw_get_status,
565 .set_status = virtio_ccw_set_status,
566 .reset = virtio_ccw_reset,
567 .find_vqs = virtio_ccw_find_vqs,
568 .del_vqs = virtio_ccw_del_vqs,
569};
570
571
572/*
573 * ccw bus driver related functions
574 */
575
576static void virtio_ccw_release_dev(struct device *_d)
577{
578 struct virtio_device *dev = container_of(_d, struct virtio_device,
579 dev);
580 struct virtio_ccw_device *vcdev = to_vc_device(dev);
581
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100582 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100583 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100584 kfree(vcdev);
585}
586
587static int irb_is_error(struct irb *irb)
588{
589 if (scsw_cstat(&irb->scsw) != 0)
590 return 1;
591 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
592 return 1;
593 if (scsw_cc(&irb->scsw) != 0)
594 return 1;
595 return 0;
596}
597
598static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
599 int index)
600{
601 struct virtio_ccw_vq_info *info;
602 unsigned long flags;
603 struct virtqueue *vq;
604
605 vq = NULL;
606 spin_lock_irqsave(&vcdev->lock, flags);
607 list_for_each_entry(info, &vcdev->virtqueues, node) {
608 if (virtqueue_get_queue_index(info->vq) == index) {
609 vq = info->vq;
610 break;
611 }
612 }
613 spin_unlock_irqrestore(&vcdev->lock, flags);
614 return vq;
615}
616
617static void virtio_ccw_int_handler(struct ccw_device *cdev,
618 unsigned long intparm,
619 struct irb *irb)
620{
621 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
622 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
623 int i;
624 struct virtqueue *vq;
625 struct virtio_driver *drv;
626
627 /* Check if it's a notification from the host. */
628 if ((intparm == 0) &&
629 (scsw_stctl(&irb->scsw) ==
630 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
631 /* OK */
632 }
633 if (irb_is_error(irb))
634 vcdev->err = -EIO; /* XXX - use real error */
635 if (vcdev->curr_io & activity) {
636 switch (activity) {
637 case VIRTIO_CCW_DOING_READ_FEAT:
638 case VIRTIO_CCW_DOING_WRITE_FEAT:
639 case VIRTIO_CCW_DOING_READ_CONFIG:
640 case VIRTIO_CCW_DOING_WRITE_CONFIG:
641 case VIRTIO_CCW_DOING_WRITE_STATUS:
642 case VIRTIO_CCW_DOING_SET_VQ:
643 case VIRTIO_CCW_DOING_SET_IND:
644 case VIRTIO_CCW_DOING_SET_CONF_IND:
645 case VIRTIO_CCW_DOING_RESET:
646 case VIRTIO_CCW_DOING_READ_VQ_CONF:
647 vcdev->curr_io &= ~activity;
648 wake_up(&vcdev->wait_q);
649 break;
650 default:
651 /* don't know what to do... */
652 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
653 activity);
654 WARN_ON(1);
655 break;
656 }
657 }
658 for_each_set_bit(i, &vcdev->indicators,
659 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
660 /* The bit clear must happen before the vring kick. */
661 clear_bit(i, &vcdev->indicators);
662 barrier();
663 vq = virtio_ccw_vq_by_ind(vcdev, i);
664 vring_interrupt(0, vq);
665 }
666 if (test_bit(0, &vcdev->indicators2)) {
667 drv = container_of(vcdev->vdev.dev.driver,
668 struct virtio_driver, driver);
669
670 if (drv && drv->config_changed)
671 drv->config_changed(&vcdev->vdev);
672 clear_bit(0, &vcdev->indicators2);
673 }
674}
675
676/*
677 * We usually want to autoonline all devices, but give the admin
678 * a way to exempt devices from this.
679 */
680#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
681 (8*sizeof(long)))
682static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
683
684static char *no_auto = "";
685
686module_param(no_auto, charp, 0444);
687MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
688
689static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
690{
691 struct ccw_dev_id id;
692
693 ccw_device_get_id(cdev, &id);
694 if (test_bit(id.devno, devs_no_auto[id.ssid]))
695 return 0;
696 return 1;
697}
698
699static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
700{
701 struct ccw_device *cdev = data;
702 int ret;
703
704 ret = ccw_device_set_online(cdev);
705 if (ret)
706 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
707}
708
709static int virtio_ccw_probe(struct ccw_device *cdev)
710{
711 cdev->handler = virtio_ccw_int_handler;
712
713 if (virtio_ccw_check_autoonline(cdev))
714 async_schedule(virtio_ccw_auto_online, cdev);
715 return 0;
716}
717
718static void virtio_ccw_remove(struct ccw_device *cdev)
719{
720 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
721
722 if (cdev->online) {
723 unregister_virtio_device(&vcdev->vdev);
724 dev_set_drvdata(&cdev->dev, NULL);
725 }
726 cdev->handler = NULL;
727}
728
729static int virtio_ccw_offline(struct ccw_device *cdev)
730{
731 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
732
733 unregister_virtio_device(&vcdev->vdev);
734 dev_set_drvdata(&cdev->dev, NULL);
735 return 0;
736}
737
738
Cornelia Huck7e64e052012-12-14 17:02:18 +0100739static int virtio_ccw_online(struct ccw_device *cdev)
740{
741 int ret;
742 struct virtio_ccw_device *vcdev;
743
744 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
745 if (!vcdev) {
746 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
747 ret = -ENOMEM;
748 goto out_free;
749 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100750 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
751 GFP_DMA | GFP_KERNEL);
752 if (!vcdev->config_block) {
753 ret = -ENOMEM;
754 goto out_free;
755 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100756 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
757 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100758 ret = -ENOMEM;
759 goto out_free;
760 }
761
762 vcdev->vdev.dev.parent = &cdev->dev;
763 vcdev->vdev.dev.release = virtio_ccw_release_dev;
764 vcdev->vdev.config = &virtio_ccw_config_ops;
765 vcdev->cdev = cdev;
766 init_waitqueue_head(&vcdev->wait_q);
767 INIT_LIST_HEAD(&vcdev->virtqueues);
768 spin_lock_init(&vcdev->lock);
769
770 dev_set_drvdata(&cdev->dev, vcdev);
771 vcdev->vdev.id.vendor = cdev->id.cu_type;
772 vcdev->vdev.id.device = cdev->id.cu_model;
773 ret = register_virtio_device(&vcdev->vdev);
774 if (ret) {
775 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
776 ret);
777 goto out_put;
778 }
779 return 0;
780out_put:
781 dev_set_drvdata(&cdev->dev, NULL);
782 put_device(&vcdev->vdev.dev);
783 return ret;
784out_free:
785 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100786 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100787 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100788 }
789 kfree(vcdev);
790 return ret;
791}
792
793static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
794{
795 /* TODO: Check whether we need special handling here. */
796 return 0;
797}
798
799static struct ccw_device_id virtio_ids[] = {
800 { CCW_DEVICE(0x3832, 0) },
801 {},
802};
803MODULE_DEVICE_TABLE(ccw, virtio_ids);
804
805static struct ccw_driver virtio_ccw_driver = {
806 .driver = {
807 .owner = THIS_MODULE,
808 .name = "virtio_ccw",
809 },
810 .ids = virtio_ids,
811 .probe = virtio_ccw_probe,
812 .remove = virtio_ccw_remove,
813 .set_offline = virtio_ccw_offline,
814 .set_online = virtio_ccw_online,
815 .notify = virtio_ccw_cio_notify,
816 .int_class = IOINT_VIR,
817};
818
819static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
820 int max_digit, int max_val)
821{
822 int diff;
823
824 diff = 0;
825 *val = 0;
826
827 while (diff <= max_digit) {
828 int value = hex_to_bin(**cp);
829
830 if (value < 0)
831 break;
832 *val = *val * 16 + value;
833 (*cp)++;
834 diff++;
835 }
836
837 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
838 return 1;
839
840 return 0;
841}
842
843static int __init parse_busid(char *str, unsigned int *cssid,
844 unsigned int *ssid, unsigned int *devno)
845{
846 char *str_work;
847 int rc, ret;
848
849 rc = 1;
850
851 if (*str == '\0')
852 goto out;
853
854 str_work = str;
855 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
856 if (ret || (str_work[0] != '.'))
857 goto out;
858 str_work++;
859 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
860 if (ret || (str_work[0] != '.'))
861 goto out;
862 str_work++;
863 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
864 if (ret || (str_work[0] != '\0'))
865 goto out;
866
867 rc = 0;
868out:
869 return rc;
870}
871
872static void __init no_auto_parse(void)
873{
874 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
875 char *parm, *str;
876 int rc;
877
878 str = no_auto;
879 while ((parm = strsep(&str, ","))) {
880 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
881 &from_ssid, &from);
882 if (rc)
883 continue;
884 if (parm != NULL) {
885 rc = parse_busid(parm, &to_cssid,
886 &to_ssid, &to);
887 if ((from_ssid > to_ssid) ||
888 ((from_ssid == to_ssid) && (from > to)))
889 rc = -EINVAL;
890 } else {
891 to_cssid = from_cssid;
892 to_ssid = from_ssid;
893 to = from;
894 }
895 if (rc)
896 continue;
897 while ((from_ssid < to_ssid) ||
898 ((from_ssid == to_ssid) && (from <= to))) {
899 set_bit(from, devs_no_auto[from_ssid]);
900 from++;
901 if (from > __MAX_SUBCHANNEL) {
902 from_ssid++;
903 from = 0;
904 }
905 }
906 }
907}
908
909static int __init virtio_ccw_init(void)
910{
911 /* parse no_auto string before we do anything further */
912 no_auto_parse();
913 return ccw_driver_register(&virtio_ccw_driver);
914}
915module_init(virtio_ccw_init);
916
917static void __exit virtio_ccw_exit(void)
918{
919 ccw_driver_unregister(&virtio_ccw_driver);
920}
921module_exit(virtio_ccw_exit);