blob: 3d657522b3e284179710652e9cfedf87663d8620 [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;
Michael S. Tsirkin07e16932013-02-28 12:33:16 +010080 long cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +010081};
82
83#define KVM_VIRTIO_CCW_RING_ALIGN 4096
84
85#define KVM_S390_VIRTIO_CCW_NOTIFY 3
86
87#define CCW_CMD_SET_VQ 0x13
88#define CCW_CMD_VDEV_RESET 0x33
89#define CCW_CMD_SET_IND 0x43
90#define CCW_CMD_SET_CONF_IND 0x53
91#define CCW_CMD_READ_FEAT 0x12
92#define CCW_CMD_WRITE_FEAT 0x11
93#define CCW_CMD_READ_CONF 0x22
94#define CCW_CMD_WRITE_CONF 0x21
95#define CCW_CMD_WRITE_STATUS 0x31
96#define CCW_CMD_READ_VQ_CONF 0x32
97
98#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
99#define VIRTIO_CCW_DOING_RESET 0x00040000
100#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
101#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
102#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
103#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
104#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
105#define VIRTIO_CCW_DOING_SET_IND 0x01000000
106#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
107#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
108#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
109
110static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
111{
112 return container_of(vdev, struct virtio_ccw_device, vdev);
113}
114
115static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
116{
117 unsigned long flags;
118 __u32 ret;
119
120 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
121 if (vcdev->err)
122 ret = 0;
123 else
124 ret = vcdev->curr_io & flag;
125 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
126 return ret;
127}
128
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100129static int ccw_io_helper(struct virtio_ccw_device *vcdev,
130 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100131{
132 int ret;
133 unsigned long flags;
134 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
135
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100136 do {
137 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
138 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
139 if (!ret)
140 vcdev->curr_io |= flag;
141 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
142 cpu_relax();
143 } while (ret == -EBUSY);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100144 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
145 return ret ? ret : vcdev->err;
146}
147
148static inline long do_kvm_notify(struct subchannel_id schid,
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100149 unsigned long queue_index,
150 long cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100151{
152 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
153 register struct subchannel_id __schid asm("2") = schid;
154 register unsigned long __index asm("3") = queue_index;
155 register long __rc asm("2");
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100156 register long __cookie asm("4") = cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100157
158 asm volatile ("diag 2,4,0x500\n"
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100159 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
160 "d"(__cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100161 : "memory", "cc");
162 return __rc;
163}
164
165static void virtio_ccw_kvm_notify(struct virtqueue *vq)
166{
167 struct virtio_ccw_vq_info *info = vq->priv;
168 struct virtio_ccw_device *vcdev;
169 struct subchannel_id schid;
170
171 vcdev = to_vc_device(info->vq->vdev);
172 ccw_device_get_schid(vcdev->cdev, &schid);
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100173 info->cookie = do_kvm_notify(schid, virtqueue_get_queue_index(vq),
174 info->cookie);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100175}
176
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100177static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
178 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100179{
180 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100181 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
182 ccw->flags = 0;
183 ccw->count = sizeof(struct vq_config_block);
184 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
185 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100186 return vcdev->config_block->num;
187}
188
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100189static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100190{
191 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
192 struct virtio_ccw_vq_info *info = vq->priv;
193 unsigned long flags;
194 unsigned long size;
195 int ret;
196 unsigned int index = virtqueue_get_queue_index(vq);
197
198 /* Remove from our list. */
199 spin_lock_irqsave(&vcdev->lock, flags);
200 list_del(&info->node);
201 spin_unlock_irqrestore(&vcdev->lock, flags);
202
203 /* Release from host. */
204 info->info_block->queue = 0;
205 info->info_block->align = 0;
206 info->info_block->index = index;
207 info->info_block->num = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100208 ccw->cmd_code = CCW_CMD_SET_VQ;
209 ccw->flags = 0;
210 ccw->count = sizeof(*info->info_block);
211 ccw->cda = (__u32)(unsigned long)(info->info_block);
212 ret = ccw_io_helper(vcdev, ccw,
213 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100214 /*
215 * -ENODEV isn't considered an error: The device is gone anyway.
216 * This may happen on device detach.
217 */
218 if (ret && (ret != -ENODEV))
219 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
220 ret, index);
221
222 vring_del_virtqueue(vq);
223 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
224 free_pages_exact(info->queue, size);
225 kfree(info->info_block);
226 kfree(info);
227}
228
229static void virtio_ccw_del_vqs(struct virtio_device *vdev)
230{
231 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100232 struct ccw1 *ccw;
233
234 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
235 if (!ccw)
236 return;
237
Cornelia Huck7e64e052012-12-14 17:02:18 +0100238
239 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100240 virtio_ccw_del_vq(vq, ccw);
241
242 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100243}
244
245static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
246 int i, vq_callback_t *callback,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100247 const char *name,
248 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100249{
250 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
251 int err;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100252 struct virtqueue *vq = NULL;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100253 struct virtio_ccw_vq_info *info;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100254 unsigned long size = 0; /* silence the compiler */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100255 unsigned long flags;
256
257 /* Allocate queue. */
258 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
259 if (!info) {
260 dev_warn(&vcdev->cdev->dev, "no info\n");
261 err = -ENOMEM;
262 goto out_err;
263 }
264 info->info_block = kzalloc(sizeof(*info->info_block),
265 GFP_DMA | GFP_KERNEL);
266 if (!info->info_block) {
267 dev_warn(&vcdev->cdev->dev, "no info block\n");
268 err = -ENOMEM;
269 goto out_err;
270 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100271 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100272 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
273 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
274 if (info->queue == NULL) {
275 dev_warn(&vcdev->cdev->dev, "no queue\n");
276 err = -ENOMEM;
277 goto out_err;
278 }
279
280 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
281 true, info->queue, virtio_ccw_kvm_notify,
282 callback, name);
283 if (!vq) {
284 /* For now, we fail if we can't get the requested size. */
285 dev_warn(&vcdev->cdev->dev, "no vq\n");
286 err = -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100287 goto out_err;
288 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100289
290 /* Register it with the host. */
291 info->info_block->queue = (__u64)info->queue;
292 info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
293 info->info_block->index = i;
294 info->info_block->num = info->num;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100295 ccw->cmd_code = CCW_CMD_SET_VQ;
296 ccw->flags = 0;
297 ccw->count = sizeof(*info->info_block);
298 ccw->cda = (__u32)(unsigned long)(info->info_block);
299 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100300 if (err) {
301 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
Cornelia Huck7e64e052012-12-14 17:02:18 +0100302 goto out_err;
303 }
304
Cornelia Huckc98d3682013-01-25 15:34:16 +0100305 info->vq = vq;
306 vq->priv = info;
307
Cornelia Huck7e64e052012-12-14 17:02:18 +0100308 /* Save it to our list. */
309 spin_lock_irqsave(&vcdev->lock, flags);
310 list_add(&info->node, &vcdev->virtqueues);
311 spin_unlock_irqrestore(&vcdev->lock, flags);
312
313 return vq;
314
315out_err:
Cornelia Huckc98d3682013-01-25 15:34:16 +0100316 if (vq)
317 vring_del_virtqueue(vq);
318 if (info) {
319 if (info->queue)
320 free_pages_exact(info->queue, size);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100321 kfree(info->info_block);
Cornelia Huckc98d3682013-01-25 15:34:16 +0100322 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100323 kfree(info);
324 return ERR_PTR(err);
325}
326
327static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
328 struct virtqueue *vqs[],
329 vq_callback_t *callbacks[],
330 const char *names[])
331{
332 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
333 unsigned long *indicatorp = NULL;
334 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100335 struct ccw1 *ccw;
336
337 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
338 if (!ccw)
339 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100340
341 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100342 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
343 ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100344 if (IS_ERR(vqs[i])) {
345 ret = PTR_ERR(vqs[i]);
346 vqs[i] = NULL;
347 goto out;
348 }
349 }
350 ret = -ENOMEM;
351 /* We need a data area under 2G to communicate. */
352 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
353 if (!indicatorp)
354 goto out;
355 *indicatorp = (unsigned long) &vcdev->indicators;
356 /* Register queue indicators with host. */
357 vcdev->indicators = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100358 ccw->cmd_code = CCW_CMD_SET_IND;
359 ccw->flags = 0;
360 ccw->count = sizeof(vcdev->indicators);
361 ccw->cda = (__u32)(unsigned long) indicatorp;
362 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100363 if (ret)
364 goto out;
365 /* Register indicators2 with host for config changes */
366 *indicatorp = (unsigned long) &vcdev->indicators2;
367 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100368 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
369 ccw->flags = 0;
370 ccw->count = sizeof(vcdev->indicators2);
371 ccw->cda = (__u32)(unsigned long) indicatorp;
372 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100373 if (ret)
374 goto out;
375
376 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100377 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100378 return 0;
379out:
380 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100381 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100382 virtio_ccw_del_vqs(vdev);
383 return ret;
384}
385
386static void virtio_ccw_reset(struct virtio_device *vdev)
387{
388 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100389 struct ccw1 *ccw;
390
391 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
392 if (!ccw)
393 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100394
395 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100396 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100397
398 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100399 ccw->cmd_code = CCW_CMD_VDEV_RESET;
400 ccw->flags = 0;
401 ccw->count = 0;
402 ccw->cda = 0;
403 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
404 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100405}
406
407static u32 virtio_ccw_get_features(struct virtio_device *vdev)
408{
409 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100410 struct virtio_feature_desc *features;
411 int ret, rc;
412 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100413
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100414 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
415 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100416 return 0;
417
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100418 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
419 if (!features) {
420 rc = 0;
421 goto out_free;
422 }
423 /* Read the feature bits from the host. */
424 /* TODO: Features > 32 bits */
425 features->index = 0;
426 ccw->cmd_code = CCW_CMD_READ_FEAT;
427 ccw->flags = 0;
428 ccw->count = sizeof(*features);
429 ccw->cda = (__u32)(unsigned long)features;
430 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
431 if (ret) {
432 rc = 0;
433 goto out_free;
434 }
435
436 rc = le32_to_cpu(features->features);
437
438out_free:
439 kfree(features);
440 kfree(ccw);
441 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100442}
443
444static void virtio_ccw_finalize_features(struct virtio_device *vdev)
445{
446 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100447 struct virtio_feature_desc *features;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100448 int i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100449 struct ccw1 *ccw;
450
451 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
452 if (!ccw)
453 return;
454
455 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
456 if (!features)
457 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100458
459 /* Give virtio_ring a chance to accept features. */
460 vring_transport_features(vdev);
461
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100462 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100463 i++) {
464 int highbits = i % 2 ? 32 : 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100465 features->index = i;
466 features->features = cpu_to_le32(vdev->features[i / 2]
467 >> highbits);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100468 /* Write the feature bits to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100469 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
470 ccw->flags = 0;
471 ccw->count = sizeof(*features);
472 ccw->cda = (__u32)(unsigned long)features;
473 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100474 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100475out_free:
476 kfree(features);
477 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100478}
479
480static void virtio_ccw_get_config(struct virtio_device *vdev,
481 unsigned int offset, void *buf, unsigned len)
482{
483 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
484 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100485 struct ccw1 *ccw;
486 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100487
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100488 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
489 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100490 return;
491
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100492 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
493 if (!config_area)
494 goto out_free;
495
496 /* Read the config area from the host. */
497 ccw->cmd_code = CCW_CMD_READ_CONF;
498 ccw->flags = 0;
499 ccw->count = offset + len;
500 ccw->cda = (__u32)(unsigned long)config_area;
501 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
502 if (ret)
503 goto out_free;
504
505 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
Cornelia Huck7e64e052012-12-14 17:02:18 +0100506 memcpy(buf, &vcdev->config[offset], len);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100507
508out_free:
509 kfree(config_area);
510 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100511}
512
513static void virtio_ccw_set_config(struct virtio_device *vdev,
514 unsigned int offset, const void *buf,
515 unsigned len)
516{
517 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100518 struct ccw1 *ccw;
519 void *config_area;
520
521 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
522 if (!ccw)
523 return;
524
525 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
526 if (!config_area)
527 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100528
529 memcpy(&vcdev->config[offset], buf, len);
530 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100531 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
532 ccw->cmd_code = CCW_CMD_WRITE_CONF;
533 ccw->flags = 0;
534 ccw->count = offset + len;
535 ccw->cda = (__u32)(unsigned long)config_area;
536 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
537
538out_free:
539 kfree(config_area);
540 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100541}
542
543static u8 virtio_ccw_get_status(struct virtio_device *vdev)
544{
545 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
546
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100547 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100548}
549
550static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
551{
552 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100553 struct ccw1 *ccw;
554
555 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
556 if (!ccw)
557 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100558
559 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100560 *vcdev->status = status;
561 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
562 ccw->flags = 0;
563 ccw->count = sizeof(status);
564 ccw->cda = (__u32)(unsigned long)vcdev->status;
565 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
566 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100567}
568
569static struct virtio_config_ops virtio_ccw_config_ops = {
570 .get_features = virtio_ccw_get_features,
571 .finalize_features = virtio_ccw_finalize_features,
572 .get = virtio_ccw_get_config,
573 .set = virtio_ccw_set_config,
574 .get_status = virtio_ccw_get_status,
575 .set_status = virtio_ccw_set_status,
576 .reset = virtio_ccw_reset,
577 .find_vqs = virtio_ccw_find_vqs,
578 .del_vqs = virtio_ccw_del_vqs,
579};
580
581
582/*
583 * ccw bus driver related functions
584 */
585
586static void virtio_ccw_release_dev(struct device *_d)
587{
588 struct virtio_device *dev = container_of(_d, struct virtio_device,
589 dev);
590 struct virtio_ccw_device *vcdev = to_vc_device(dev);
591
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100592 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100593 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100594 kfree(vcdev);
595}
596
597static int irb_is_error(struct irb *irb)
598{
599 if (scsw_cstat(&irb->scsw) != 0)
600 return 1;
601 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
602 return 1;
603 if (scsw_cc(&irb->scsw) != 0)
604 return 1;
605 return 0;
606}
607
608static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
609 int index)
610{
611 struct virtio_ccw_vq_info *info;
612 unsigned long flags;
613 struct virtqueue *vq;
614
615 vq = NULL;
616 spin_lock_irqsave(&vcdev->lock, flags);
617 list_for_each_entry(info, &vcdev->virtqueues, node) {
618 if (virtqueue_get_queue_index(info->vq) == index) {
619 vq = info->vq;
620 break;
621 }
622 }
623 spin_unlock_irqrestore(&vcdev->lock, flags);
624 return vq;
625}
626
627static void virtio_ccw_int_handler(struct ccw_device *cdev,
628 unsigned long intparm,
629 struct irb *irb)
630{
631 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
632 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
633 int i;
634 struct virtqueue *vq;
635 struct virtio_driver *drv;
636
637 /* Check if it's a notification from the host. */
638 if ((intparm == 0) &&
639 (scsw_stctl(&irb->scsw) ==
640 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
641 /* OK */
642 }
643 if (irb_is_error(irb))
644 vcdev->err = -EIO; /* XXX - use real error */
645 if (vcdev->curr_io & activity) {
646 switch (activity) {
647 case VIRTIO_CCW_DOING_READ_FEAT:
648 case VIRTIO_CCW_DOING_WRITE_FEAT:
649 case VIRTIO_CCW_DOING_READ_CONFIG:
650 case VIRTIO_CCW_DOING_WRITE_CONFIG:
651 case VIRTIO_CCW_DOING_WRITE_STATUS:
652 case VIRTIO_CCW_DOING_SET_VQ:
653 case VIRTIO_CCW_DOING_SET_IND:
654 case VIRTIO_CCW_DOING_SET_CONF_IND:
655 case VIRTIO_CCW_DOING_RESET:
656 case VIRTIO_CCW_DOING_READ_VQ_CONF:
657 vcdev->curr_io &= ~activity;
658 wake_up(&vcdev->wait_q);
659 break;
660 default:
661 /* don't know what to do... */
662 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
663 activity);
664 WARN_ON(1);
665 break;
666 }
667 }
668 for_each_set_bit(i, &vcdev->indicators,
669 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
670 /* The bit clear must happen before the vring kick. */
671 clear_bit(i, &vcdev->indicators);
672 barrier();
673 vq = virtio_ccw_vq_by_ind(vcdev, i);
674 vring_interrupt(0, vq);
675 }
676 if (test_bit(0, &vcdev->indicators2)) {
677 drv = container_of(vcdev->vdev.dev.driver,
678 struct virtio_driver, driver);
679
680 if (drv && drv->config_changed)
681 drv->config_changed(&vcdev->vdev);
682 clear_bit(0, &vcdev->indicators2);
683 }
684}
685
686/*
687 * We usually want to autoonline all devices, but give the admin
688 * a way to exempt devices from this.
689 */
690#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
691 (8*sizeof(long)))
692static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
693
694static char *no_auto = "";
695
696module_param(no_auto, charp, 0444);
697MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
698
699static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
700{
701 struct ccw_dev_id id;
702
703 ccw_device_get_id(cdev, &id);
704 if (test_bit(id.devno, devs_no_auto[id.ssid]))
705 return 0;
706 return 1;
707}
708
709static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
710{
711 struct ccw_device *cdev = data;
712 int ret;
713
714 ret = ccw_device_set_online(cdev);
715 if (ret)
716 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
717}
718
719static int virtio_ccw_probe(struct ccw_device *cdev)
720{
721 cdev->handler = virtio_ccw_int_handler;
722
723 if (virtio_ccw_check_autoonline(cdev))
724 async_schedule(virtio_ccw_auto_online, cdev);
725 return 0;
726}
727
728static void virtio_ccw_remove(struct ccw_device *cdev)
729{
730 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
731
732 if (cdev->online) {
733 unregister_virtio_device(&vcdev->vdev);
734 dev_set_drvdata(&cdev->dev, NULL);
735 }
736 cdev->handler = NULL;
737}
738
739static int virtio_ccw_offline(struct ccw_device *cdev)
740{
741 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
742
743 unregister_virtio_device(&vcdev->vdev);
744 dev_set_drvdata(&cdev->dev, NULL);
745 return 0;
746}
747
748
Cornelia Huck7e64e052012-12-14 17:02:18 +0100749static int virtio_ccw_online(struct ccw_device *cdev)
750{
751 int ret;
752 struct virtio_ccw_device *vcdev;
753
754 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
755 if (!vcdev) {
756 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
757 ret = -ENOMEM;
758 goto out_free;
759 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100760 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
761 GFP_DMA | GFP_KERNEL);
762 if (!vcdev->config_block) {
763 ret = -ENOMEM;
764 goto out_free;
765 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100766 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
767 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100768 ret = -ENOMEM;
769 goto out_free;
770 }
771
772 vcdev->vdev.dev.parent = &cdev->dev;
773 vcdev->vdev.dev.release = virtio_ccw_release_dev;
774 vcdev->vdev.config = &virtio_ccw_config_ops;
775 vcdev->cdev = cdev;
776 init_waitqueue_head(&vcdev->wait_q);
777 INIT_LIST_HEAD(&vcdev->virtqueues);
778 spin_lock_init(&vcdev->lock);
779
780 dev_set_drvdata(&cdev->dev, vcdev);
781 vcdev->vdev.id.vendor = cdev->id.cu_type;
782 vcdev->vdev.id.device = cdev->id.cu_model;
783 ret = register_virtio_device(&vcdev->vdev);
784 if (ret) {
785 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
786 ret);
787 goto out_put;
788 }
789 return 0;
790out_put:
791 dev_set_drvdata(&cdev->dev, NULL);
792 put_device(&vcdev->vdev.dev);
793 return ret;
794out_free:
795 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100796 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100797 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100798 }
799 kfree(vcdev);
800 return ret;
801}
802
803static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
804{
805 /* TODO: Check whether we need special handling here. */
806 return 0;
807}
808
809static struct ccw_device_id virtio_ids[] = {
810 { CCW_DEVICE(0x3832, 0) },
811 {},
812};
813MODULE_DEVICE_TABLE(ccw, virtio_ids);
814
815static struct ccw_driver virtio_ccw_driver = {
816 .driver = {
817 .owner = THIS_MODULE,
818 .name = "virtio_ccw",
819 },
820 .ids = virtio_ids,
821 .probe = virtio_ccw_probe,
822 .remove = virtio_ccw_remove,
823 .set_offline = virtio_ccw_offline,
824 .set_online = virtio_ccw_online,
825 .notify = virtio_ccw_cio_notify,
Linus Torvalds89f88332013-02-24 13:07:18 -0800826 .int_class = IRQIO_VIR,
Cornelia Huck7e64e052012-12-14 17:02:18 +0100827};
828
829static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
830 int max_digit, int max_val)
831{
832 int diff;
833
834 diff = 0;
835 *val = 0;
836
837 while (diff <= max_digit) {
838 int value = hex_to_bin(**cp);
839
840 if (value < 0)
841 break;
842 *val = *val * 16 + value;
843 (*cp)++;
844 diff++;
845 }
846
847 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
848 return 1;
849
850 return 0;
851}
852
853static int __init parse_busid(char *str, unsigned int *cssid,
854 unsigned int *ssid, unsigned int *devno)
855{
856 char *str_work;
857 int rc, ret;
858
859 rc = 1;
860
861 if (*str == '\0')
862 goto out;
863
864 str_work = str;
865 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
866 if (ret || (str_work[0] != '.'))
867 goto out;
868 str_work++;
869 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
870 if (ret || (str_work[0] != '.'))
871 goto out;
872 str_work++;
873 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
874 if (ret || (str_work[0] != '\0'))
875 goto out;
876
877 rc = 0;
878out:
879 return rc;
880}
881
882static void __init no_auto_parse(void)
883{
884 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
885 char *parm, *str;
886 int rc;
887
888 str = no_auto;
889 while ((parm = strsep(&str, ","))) {
890 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
891 &from_ssid, &from);
892 if (rc)
893 continue;
894 if (parm != NULL) {
895 rc = parse_busid(parm, &to_cssid,
896 &to_ssid, &to);
897 if ((from_ssid > to_ssid) ||
898 ((from_ssid == to_ssid) && (from > to)))
899 rc = -EINVAL;
900 } else {
901 to_cssid = from_cssid;
902 to_ssid = from_ssid;
903 to = from;
904 }
905 if (rc)
906 continue;
907 while ((from_ssid < to_ssid) ||
908 ((from_ssid == to_ssid) && (from <= to))) {
909 set_bit(from, devs_no_auto[from_ssid]);
910 from++;
911 if (from > __MAX_SUBCHANNEL) {
912 from_ssid++;
913 from = 0;
914 }
915 }
916 }
917}
918
919static int __init virtio_ccw_init(void)
920{
921 /* parse no_auto string before we do anything further */
922 no_auto_parse();
923 return ccw_driver_register(&virtio_ccw_driver);
924}
925module_init(virtio_ccw_init);
926
927static void __exit virtio_ccw_exit(void)
928{
929 ccw_driver_unregister(&virtio_ccw_driver);
930}
931module_exit(virtio_ccw_exit);