blob: 71d7802aa8b48b75aee89ea2aae167436c746d65 [file] [log] [blame]
Cornelia Huck7e64e052012-12-14 17:02:18 +01001/*
2 * ccw based virtio transport
3 *
Cornelia Huck96b14532013-02-06 10:23:39 +01004 * Copyright IBM Corp. 2012, 2014
Cornelia Huck7e64e052012-12-14 17:02:18 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
11 */
12
13#include <linux/kernel_stat.h>
14#include <linux/init.h>
15#include <linux/bootmem.h>
16#include <linux/err.h>
17#include <linux/virtio.h>
18#include <linux/virtio_config.h>
19#include <linux/slab.h>
20#include <linux/interrupt.h>
21#include <linux/virtio_ring.h>
22#include <linux/pfn.h>
23#include <linux/async.h>
24#include <linux/wait.h>
25#include <linux/list.h>
26#include <linux/bitops.h>
27#include <linux/module.h>
28#include <linux/io.h>
29#include <linux/kvm_para.h>
Heinz Graalfse75279c2014-04-28 11:24:05 +093030#include <linux/notifier.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010031#include <asm/setup.h>
32#include <asm/irq.h>
33#include <asm/cio.h>
34#include <asm/ccwdev.h>
Cornelia Huck6a773cb2013-02-28 12:33:17 +010035#include <asm/virtio-ccw.h>
Cornelia Huck96b14532013-02-06 10:23:39 +010036#include <asm/isc.h>
37#include <asm/airq.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010038
39/*
40 * virtio related functions
41 */
42
43struct vq_config_block {
44 __u16 index;
45 __u16 num;
46} __packed;
47
48#define VIRTIO_CCW_CONFIG_SIZE 0x100
49/* same as PCI config space size, should be enough for all drivers */
50
51struct virtio_ccw_device {
52 struct virtio_device vdev;
Cornelia Huck73fa21e2013-01-07 15:51:51 +010053 __u8 *status;
Cornelia Huck7e64e052012-12-14 17:02:18 +010054 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
55 struct ccw_device *cdev;
Cornelia Huck7e64e052012-12-14 17:02:18 +010056 __u32 curr_io;
57 int err;
Thomas Huth6bb2c832014-10-07 16:39:50 +020058 unsigned int revision; /* Transport revision */
Cornelia Huck7e64e052012-12-14 17:02:18 +010059 wait_queue_head_t wait_q;
60 spinlock_t lock;
61 struct list_head virtqueues;
62 unsigned long indicators;
63 unsigned long indicators2;
64 struct vq_config_block *config_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010065 bool is_thinint;
Heinz Graalfs79629b22014-03-05 15:23:54 +010066 bool going_away;
Heinz Graalfse75279c2014-04-28 11:24:05 +093067 bool device_lost;
Cornelia Huck96b14532013-02-06 10:23:39 +010068 void *airq_info;
Cornelia Huck7e64e052012-12-14 17:02:18 +010069};
70
Cornelia Huckd4674242014-10-07 16:39:51 +020071struct vq_info_block_legacy {
Cornelia Huck7e64e052012-12-14 17:02:18 +010072 __u64 queue;
73 __u32 align;
74 __u16 index;
75 __u16 num;
76} __packed;
77
Cornelia Huckd4674242014-10-07 16:39:51 +020078struct vq_info_block {
79 __u64 desc;
80 __u32 res0;
81 __u16 index;
82 __u16 num;
83 __u64 avail;
84 __u64 used;
85} __packed;
86
Cornelia Huck7e64e052012-12-14 17:02:18 +010087struct virtio_feature_desc {
88 __u32 features;
89 __u8 index;
90} __packed;
91
Cornelia Huck96b14532013-02-06 10:23:39 +010092struct virtio_thinint_area {
93 unsigned long summary_indicator;
94 unsigned long indicator;
95 u64 bit_nr;
96 u8 isc;
97} __packed;
98
Thomas Huth6bb2c832014-10-07 16:39:50 +020099struct virtio_rev_info {
100 __u16 revision;
101 __u16 length;
102 __u8 data[];
103};
104
105/* the highest virtio-ccw revision we support */
Cornelia Huckaf9ca132014-10-07 16:39:52 +0200106#define VIRTIO_CCW_REV_MAX 1
Thomas Huth6bb2c832014-10-07 16:39:50 +0200107
Cornelia Huck7e64e052012-12-14 17:02:18 +0100108struct virtio_ccw_vq_info {
109 struct virtqueue *vq;
110 int num;
111 void *queue;
Cornelia Huckd4674242014-10-07 16:39:51 +0200112 union {
113 struct vq_info_block s;
114 struct vq_info_block_legacy l;
115 } *info_block;
Cornelia Huck96b14532013-02-06 10:23:39 +0100116 int bit_nr;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100117 struct list_head node;
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100118 long cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100119};
120
Cornelia Huck96b14532013-02-06 10:23:39 +0100121#define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
122
123#define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
124#define MAX_AIRQ_AREAS 20
125
126static int virtio_ccw_use_airq = 1;
127
128struct airq_info {
129 rwlock_t lock;
130 u8 summary_indicator;
131 struct airq_struct airq;
132 struct airq_iv *aiv;
133};
134static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
135
Cornelia Huck7e64e052012-12-14 17:02:18 +0100136#define CCW_CMD_SET_VQ 0x13
137#define CCW_CMD_VDEV_RESET 0x33
138#define CCW_CMD_SET_IND 0x43
139#define CCW_CMD_SET_CONF_IND 0x53
140#define CCW_CMD_READ_FEAT 0x12
141#define CCW_CMD_WRITE_FEAT 0x11
142#define CCW_CMD_READ_CONF 0x22
143#define CCW_CMD_WRITE_CONF 0x21
144#define CCW_CMD_WRITE_STATUS 0x31
145#define CCW_CMD_READ_VQ_CONF 0x32
Cornelia Huck96b14532013-02-06 10:23:39 +0100146#define CCW_CMD_SET_IND_ADAPTER 0x73
Thomas Huth6bb2c832014-10-07 16:39:50 +0200147#define CCW_CMD_SET_VIRTIO_REV 0x83
Cornelia Huck7e64e052012-12-14 17:02:18 +0100148
149#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
150#define VIRTIO_CCW_DOING_RESET 0x00040000
151#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
152#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
153#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
154#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
155#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
156#define VIRTIO_CCW_DOING_SET_IND 0x01000000
157#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
158#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
Cornelia Huck96b14532013-02-06 10:23:39 +0100159#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
Thomas Huth6bb2c832014-10-07 16:39:50 +0200160#define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
Cornelia Huck7e64e052012-12-14 17:02:18 +0100161#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
162
163static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
164{
165 return container_of(vdev, struct virtio_ccw_device, vdev);
166}
167
Cornelia Huck96b14532013-02-06 10:23:39 +0100168static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
169{
170 unsigned long i, flags;
171
172 write_lock_irqsave(&info->lock, flags);
173 for (i = 0; i < airq_iv_end(info->aiv); i++) {
174 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
175 airq_iv_free_bit(info->aiv, i);
176 airq_iv_set_ptr(info->aiv, i, 0);
177 break;
178 }
179 }
180 write_unlock_irqrestore(&info->lock, flags);
181}
182
183static void virtio_airq_handler(struct airq_struct *airq)
184{
185 struct airq_info *info = container_of(airq, struct airq_info, airq);
186 unsigned long ai;
187
188 inc_irq_stat(IRQIO_VAI);
189 read_lock(&info->lock);
190 /* Walk through indicators field, summary indicator active. */
191 for (ai = 0;;) {
192 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
193 if (ai == -1UL)
194 break;
195 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
196 }
197 info->summary_indicator = 0;
198 smp_wmb();
199 /* Walk through indicators field, summary indicator not active. */
200 for (ai = 0;;) {
201 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
202 if (ai == -1UL)
203 break;
204 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
205 }
206 read_unlock(&info->lock);
207}
208
209static struct airq_info *new_airq_info(void)
210{
211 struct airq_info *info;
212 int rc;
213
214 info = kzalloc(sizeof(*info), GFP_KERNEL);
215 if (!info)
216 return NULL;
217 rwlock_init(&info->lock);
218 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
219 if (!info->aiv) {
220 kfree(info);
221 return NULL;
222 }
223 info->airq.handler = virtio_airq_handler;
224 info->airq.lsi_ptr = &info->summary_indicator;
225 info->airq.lsi_mask = 0xff;
226 info->airq.isc = VIRTIO_AIRQ_ISC;
227 rc = register_adapter_interrupt(&info->airq);
228 if (rc) {
229 airq_iv_release(info->aiv);
230 kfree(info);
231 return NULL;
232 }
233 return info;
234}
235
236static void destroy_airq_info(struct airq_info *info)
237{
238 if (!info)
239 return;
240
241 unregister_adapter_interrupt(&info->airq);
242 airq_iv_release(info->aiv);
243 kfree(info);
244}
245
246static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
247 u64 *first, void **airq_info)
248{
249 int i, j;
250 struct airq_info *info;
251 unsigned long indicator_addr = 0;
252 unsigned long bit, flags;
253
254 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
255 if (!airq_areas[i])
256 airq_areas[i] = new_airq_info();
257 info = airq_areas[i];
258 if (!info)
259 return 0;
260 write_lock_irqsave(&info->lock, flags);
261 bit = airq_iv_alloc(info->aiv, nvqs);
262 if (bit == -1UL) {
263 /* Not enough vacancies. */
264 write_unlock_irqrestore(&info->lock, flags);
265 continue;
266 }
267 *first = bit;
268 *airq_info = info;
269 indicator_addr = (unsigned long)info->aiv->vector;
270 for (j = 0; j < nvqs; j++) {
271 airq_iv_set_ptr(info->aiv, bit + j,
272 (unsigned long)vqs[j]);
273 }
274 write_unlock_irqrestore(&info->lock, flags);
275 }
276 return indicator_addr;
277}
278
279static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
280{
281 struct virtio_ccw_vq_info *info;
282
283 list_for_each_entry(info, &vcdev->virtqueues, node)
284 drop_airq_indicator(info->vq, vcdev->airq_info);
285}
286
Cornelia Huck7e64e052012-12-14 17:02:18 +0100287static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
288{
289 unsigned long flags;
290 __u32 ret;
291
292 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
293 if (vcdev->err)
294 ret = 0;
295 else
296 ret = vcdev->curr_io & flag;
297 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
298 return ret;
299}
300
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100301static int ccw_io_helper(struct virtio_ccw_device *vcdev,
302 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100303{
304 int ret;
305 unsigned long flags;
306 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
307
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100308 do {
309 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
310 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
Cornelia Huck99437a22013-04-04 10:25:06 +0200311 if (!ret) {
312 if (!vcdev->curr_io)
313 vcdev->err = 0;
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100314 vcdev->curr_io |= flag;
Cornelia Huck99437a22013-04-04 10:25:06 +0200315 }
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100316 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
317 cpu_relax();
318 } while (ret == -EBUSY);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100319 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
320 return ret ? ret : vcdev->err;
321}
322
Cornelia Huck96b14532013-02-06 10:23:39 +0100323static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
324 struct ccw1 *ccw)
325{
326 int ret;
327 unsigned long *indicatorp = NULL;
328 struct virtio_thinint_area *thinint_area = NULL;
329 struct airq_info *airq_info = vcdev->airq_info;
330
331 if (vcdev->is_thinint) {
332 thinint_area = kzalloc(sizeof(*thinint_area),
333 GFP_DMA | GFP_KERNEL);
334 if (!thinint_area)
335 return;
336 thinint_area->summary_indicator =
337 (unsigned long) &airq_info->summary_indicator;
338 thinint_area->isc = VIRTIO_AIRQ_ISC;
339 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
340 ccw->count = sizeof(*thinint_area);
341 ccw->cda = (__u32)(unsigned long) thinint_area;
342 } else {
343 indicatorp = kmalloc(sizeof(&vcdev->indicators),
344 GFP_DMA | GFP_KERNEL);
345 if (!indicatorp)
346 return;
347 *indicatorp = 0;
348 ccw->cmd_code = CCW_CMD_SET_IND;
349 ccw->count = sizeof(vcdev->indicators);
350 ccw->cda = (__u32)(unsigned long) indicatorp;
351 }
352 /* Deregister indicators from host. */
353 vcdev->indicators = 0;
354 ccw->flags = 0;
355 ret = ccw_io_helper(vcdev, ccw,
356 vcdev->is_thinint ?
357 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
358 VIRTIO_CCW_DOING_SET_IND);
359 if (ret && (ret != -ENODEV))
360 dev_info(&vcdev->cdev->dev,
361 "Failed to deregister indicators (%d)\n", ret);
362 else if (vcdev->is_thinint)
363 virtio_ccw_drop_indicators(vcdev);
364 kfree(indicatorp);
365 kfree(thinint_area);
366}
367
Cornelia Huck7e64e052012-12-14 17:02:18 +0100368static inline long do_kvm_notify(struct subchannel_id schid,
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100369 unsigned long queue_index,
370 long cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100371{
372 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
373 register struct subchannel_id __schid asm("2") = schid;
374 register unsigned long __index asm("3") = queue_index;
375 register long __rc asm("2");
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100376 register long __cookie asm("4") = cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100377
378 asm volatile ("diag 2,4,0x500\n"
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100379 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
380 "d"(__cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100381 : "memory", "cc");
382 return __rc;
383}
384
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030385static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100386{
387 struct virtio_ccw_vq_info *info = vq->priv;
388 struct virtio_ccw_device *vcdev;
389 struct subchannel_id schid;
390
391 vcdev = to_vc_device(info->vq->vdev);
392 ccw_device_get_schid(vcdev->cdev, &schid);
Linus Torvalds01227a82013-05-05 14:47:31 -0700393 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030394 if (info->cookie < 0)
395 return false;
396 return true;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100397}
398
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100399static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
400 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100401{
402 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100403 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
404 ccw->flags = 0;
405 ccw->count = sizeof(struct vq_config_block);
406 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
407 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100408 return vcdev->config_block->num;
409}
410
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100411static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100412{
413 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
414 struct virtio_ccw_vq_info *info = vq->priv;
415 unsigned long flags;
416 unsigned long size;
417 int ret;
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000418 unsigned int index = vq->index;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100419
420 /* Remove from our list. */
421 spin_lock_irqsave(&vcdev->lock, flags);
422 list_del(&info->node);
423 spin_unlock_irqrestore(&vcdev->lock, flags);
424
425 /* Release from host. */
Cornelia Huckd4674242014-10-07 16:39:51 +0200426 if (vcdev->revision == 0) {
427 info->info_block->l.queue = 0;
428 info->info_block->l.align = 0;
429 info->info_block->l.index = index;
430 info->info_block->l.num = 0;
431 ccw->count = sizeof(info->info_block->l);
432 } else {
433 info->info_block->s.desc = 0;
434 info->info_block->s.index = index;
435 info->info_block->s.num = 0;
436 info->info_block->s.avail = 0;
437 info->info_block->s.used = 0;
438 ccw->count = sizeof(info->info_block->s);
439 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100440 ccw->cmd_code = CCW_CMD_SET_VQ;
441 ccw->flags = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100442 ccw->cda = (__u32)(unsigned long)(info->info_block);
443 ret = ccw_io_helper(vcdev, ccw,
444 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100445 /*
446 * -ENODEV isn't considered an error: The device is gone anyway.
447 * This may happen on device detach.
448 */
449 if (ret && (ret != -ENODEV))
450 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
451 ret, index);
452
453 vring_del_virtqueue(vq);
454 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
455 free_pages_exact(info->queue, size);
456 kfree(info->info_block);
457 kfree(info);
458}
459
460static void virtio_ccw_del_vqs(struct virtio_device *vdev)
461{
462 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100463 struct ccw1 *ccw;
Cornelia Huck96b14532013-02-06 10:23:39 +0100464 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100465
466 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
467 if (!ccw)
468 return;
469
Cornelia Huck96b14532013-02-06 10:23:39 +0100470 virtio_ccw_drop_indicator(vcdev, ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100471
472 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100473 virtio_ccw_del_vq(vq, ccw);
474
475 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100476}
477
478static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
479 int i, vq_callback_t *callback,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100480 const char *name,
481 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100482{
483 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
484 int err;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100485 struct virtqueue *vq = NULL;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100486 struct virtio_ccw_vq_info *info;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100487 unsigned long size = 0; /* silence the compiler */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100488 unsigned long flags;
489
490 /* Allocate queue. */
491 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
492 if (!info) {
493 dev_warn(&vcdev->cdev->dev, "no info\n");
494 err = -ENOMEM;
495 goto out_err;
496 }
497 info->info_block = kzalloc(sizeof(*info->info_block),
498 GFP_DMA | GFP_KERNEL);
499 if (!info->info_block) {
500 dev_warn(&vcdev->cdev->dev, "no info block\n");
501 err = -ENOMEM;
502 goto out_err;
503 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100504 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100505 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
506 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
507 if (info->queue == NULL) {
508 dev_warn(&vcdev->cdev->dev, "no queue\n");
509 err = -ENOMEM;
510 goto out_err;
511 }
512
513 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
514 true, info->queue, virtio_ccw_kvm_notify,
515 callback, name);
516 if (!vq) {
517 /* For now, we fail if we can't get the requested size. */
518 dev_warn(&vcdev->cdev->dev, "no vq\n");
519 err = -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100520 goto out_err;
521 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100522
523 /* Register it with the host. */
Cornelia Huckd4674242014-10-07 16:39:51 +0200524 if (vcdev->revision == 0) {
525 info->info_block->l.queue = (__u64)info->queue;
526 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
527 info->info_block->l.index = i;
528 info->info_block->l.num = info->num;
529 ccw->count = sizeof(info->info_block->l);
530 } else {
531 info->info_block->s.desc = (__u64)info->queue;
532 info->info_block->s.index = i;
533 info->info_block->s.num = info->num;
534 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
535 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
536 ccw->count = sizeof(info->info_block->s);
537 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100538 ccw->cmd_code = CCW_CMD_SET_VQ;
539 ccw->flags = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100540 ccw->cda = (__u32)(unsigned long)(info->info_block);
541 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100542 if (err) {
543 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
Cornelia Huck7e64e052012-12-14 17:02:18 +0100544 goto out_err;
545 }
546
Cornelia Huckc98d3682013-01-25 15:34:16 +0100547 info->vq = vq;
548 vq->priv = info;
549
Cornelia Huck7e64e052012-12-14 17:02:18 +0100550 /* Save it to our list. */
551 spin_lock_irqsave(&vcdev->lock, flags);
552 list_add(&info->node, &vcdev->virtqueues);
553 spin_unlock_irqrestore(&vcdev->lock, flags);
554
555 return vq;
556
557out_err:
Cornelia Huckc98d3682013-01-25 15:34:16 +0100558 if (vq)
559 vring_del_virtqueue(vq);
560 if (info) {
561 if (info->queue)
562 free_pages_exact(info->queue, size);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100563 kfree(info->info_block);
Cornelia Huckc98d3682013-01-25 15:34:16 +0100564 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100565 kfree(info);
566 return ERR_PTR(err);
567}
568
Cornelia Huck96b14532013-02-06 10:23:39 +0100569static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
570 struct virtqueue *vqs[], int nvqs,
571 struct ccw1 *ccw)
572{
573 int ret;
574 struct virtio_thinint_area *thinint_area = NULL;
575 struct airq_info *info;
576
577 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
578 if (!thinint_area) {
579 ret = -ENOMEM;
580 goto out;
581 }
582 /* Try to get an indicator. */
583 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
584 &thinint_area->bit_nr,
585 &vcdev->airq_info);
586 if (!thinint_area->indicator) {
587 ret = -ENOSPC;
588 goto out;
589 }
590 info = vcdev->airq_info;
591 thinint_area->summary_indicator =
592 (unsigned long) &info->summary_indicator;
593 thinint_area->isc = VIRTIO_AIRQ_ISC;
594 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
595 ccw->flags = CCW_FLAG_SLI;
596 ccw->count = sizeof(*thinint_area);
597 ccw->cda = (__u32)(unsigned long)thinint_area;
598 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
599 if (ret) {
600 if (ret == -EOPNOTSUPP) {
601 /*
602 * The host does not support adapter interrupts
603 * for virtio-ccw, stop trying.
604 */
605 virtio_ccw_use_airq = 0;
606 pr_info("Adapter interrupts unsupported on host\n");
607 } else
608 dev_warn(&vcdev->cdev->dev,
609 "enabling adapter interrupts = %d\n", ret);
610 virtio_ccw_drop_indicators(vcdev);
611 }
612out:
613 kfree(thinint_area);
614 return ret;
615}
616
Cornelia Huck7e64e052012-12-14 17:02:18 +0100617static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
618 struct virtqueue *vqs[],
619 vq_callback_t *callbacks[],
620 const char *names[])
621{
622 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
623 unsigned long *indicatorp = NULL;
624 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100625 struct ccw1 *ccw;
626
627 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
628 if (!ccw)
629 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100630
631 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100632 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
633 ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100634 if (IS_ERR(vqs[i])) {
635 ret = PTR_ERR(vqs[i]);
636 vqs[i] = NULL;
637 goto out;
638 }
639 }
640 ret = -ENOMEM;
641 /* We need a data area under 2G to communicate. */
642 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
643 if (!indicatorp)
644 goto out;
645 *indicatorp = (unsigned long) &vcdev->indicators;
Cornelia Huck96b14532013-02-06 10:23:39 +0100646 if (vcdev->is_thinint) {
647 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
648 if (ret)
649 /* no error, just fall back to legacy interrupts */
650 vcdev->is_thinint = 0;
651 }
652 if (!vcdev->is_thinint) {
653 /* Register queue indicators with host. */
654 vcdev->indicators = 0;
655 ccw->cmd_code = CCW_CMD_SET_IND;
656 ccw->flags = 0;
657 ccw->count = sizeof(vcdev->indicators);
658 ccw->cda = (__u32)(unsigned long) indicatorp;
659 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
660 if (ret)
661 goto out;
662 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100663 /* Register indicators2 with host for config changes */
664 *indicatorp = (unsigned long) &vcdev->indicators2;
665 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100666 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
667 ccw->flags = 0;
668 ccw->count = sizeof(vcdev->indicators2);
669 ccw->cda = (__u32)(unsigned long) indicatorp;
670 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100671 if (ret)
672 goto out;
673
674 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100675 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100676 return 0;
677out:
678 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100679 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100680 virtio_ccw_del_vqs(vdev);
681 return ret;
682}
683
684static void virtio_ccw_reset(struct virtio_device *vdev)
685{
686 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100687 struct ccw1 *ccw;
688
689 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
690 if (!ccw)
691 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100692
693 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100694 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100695
696 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100697 ccw->cmd_code = CCW_CMD_VDEV_RESET;
698 ccw->flags = 0;
699 ccw->count = 0;
700 ccw->cda = 0;
701 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
702 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100703}
704
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200705static u64 virtio_ccw_get_features(struct virtio_device *vdev)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100706{
707 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100708 struct virtio_feature_desc *features;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200709 int ret;
710 u64 rc;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100711 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100712
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100713 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
714 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100715 return 0;
716
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100717 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
718 if (!features) {
719 rc = 0;
720 goto out_free;
721 }
722 /* Read the feature bits from the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100723 features->index = 0;
724 ccw->cmd_code = CCW_CMD_READ_FEAT;
725 ccw->flags = 0;
726 ccw->count = sizeof(*features);
727 ccw->cda = (__u32)(unsigned long)features;
728 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
729 if (ret) {
730 rc = 0;
731 goto out_free;
732 }
733
734 rc = le32_to_cpu(features->features);
735
Michael S. Tsirkince154082014-12-04 18:59:50 +0200736 if (vcdev->revision == 0)
737 goto out_free;
738
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200739 /* Read second half of the feature bits from the host. */
740 features->index = 1;
741 ccw->cmd_code = CCW_CMD_READ_FEAT;
742 ccw->flags = 0;
743 ccw->count = sizeof(*features);
744 ccw->cda = (__u32)(unsigned long)features;
745 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
746 if (ret == 0)
747 rc |= (u64)le32_to_cpu(features->features) << 32;
748
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100749out_free:
750 kfree(features);
751 kfree(ccw);
752 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100753}
754
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200755static int virtio_ccw_finalize_features(struct virtio_device *vdev)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100756{
757 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100758 struct virtio_feature_desc *features;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100759 struct ccw1 *ccw;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100760 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100761
Michael S. Tsirkinf13d8bc2014-12-09 14:46:44 +0200762 if (vcdev->revision >= 1 &&
Michael S. Tsirkin4d236762014-12-04 19:16:43 +0200763 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
764 dev_err(&vdev->dev, "virtio: device uses revision 1 "
765 "but does not have VIRTIO_F_VERSION_1\n");
766 return -EINVAL;
767 }
768
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100769 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
770 if (!ccw)
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100771 return -ENOMEM;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100772
773 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100774 if (!features) {
775 ret = -ENOMEM;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100776 goto out_free;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100777 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100778 /* Give virtio_ring a chance to accept features. */
779 vring_transport_features(vdev);
780
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200781 features->index = 0;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200782 features->features = cpu_to_le32((u32)vdev->features);
783 /* Write the first half of the feature bits to the host. */
784 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
785 ccw->flags = 0;
786 ccw->count = sizeof(*features);
787 ccw->cda = (__u32)(unsigned long)features;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100788 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
789 if (ret)
790 goto out_free;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200791
Michael S. Tsirkince154082014-12-04 18:59:50 +0200792 if (vcdev->revision == 0)
793 goto out_free;
794
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200795 features->index = 1;
796 features->features = cpu_to_le32(vdev->features >> 32);
797 /* Write the second half of the feature bits to the host. */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200798 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
799 ccw->flags = 0;
800 ccw->count = sizeof(*features);
801 ccw->cda = (__u32)(unsigned long)features;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100802 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200803
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100804out_free:
805 kfree(features);
806 kfree(ccw);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200807
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100808 return ret;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100809}
810
811static void virtio_ccw_get_config(struct virtio_device *vdev,
812 unsigned int offset, void *buf, unsigned len)
813{
814 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
815 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100816 struct ccw1 *ccw;
817 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100818
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100819 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
820 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100821 return;
822
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100823 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
824 if (!config_area)
825 goto out_free;
826
827 /* Read the config area from the host. */
828 ccw->cmd_code = CCW_CMD_READ_CONF;
829 ccw->flags = 0;
830 ccw->count = offset + len;
831 ccw->cda = (__u32)(unsigned long)config_area;
832 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
833 if (ret)
834 goto out_free;
835
836 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
Cornelia Huck7e64e052012-12-14 17:02:18 +0100837 memcpy(buf, &vcdev->config[offset], len);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100838
839out_free:
840 kfree(config_area);
841 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100842}
843
844static void virtio_ccw_set_config(struct virtio_device *vdev,
845 unsigned int offset, const void *buf,
846 unsigned len)
847{
848 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100849 struct ccw1 *ccw;
850 void *config_area;
851
852 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
853 if (!ccw)
854 return;
855
856 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
857 if (!config_area)
858 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100859
860 memcpy(&vcdev->config[offset], buf, len);
861 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100862 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
863 ccw->cmd_code = CCW_CMD_WRITE_CONF;
864 ccw->flags = 0;
865 ccw->count = offset + len;
866 ccw->cda = (__u32)(unsigned long)config_area;
867 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
868
869out_free:
870 kfree(config_area);
871 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100872}
873
874static u8 virtio_ccw_get_status(struct virtio_device *vdev)
875{
876 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
877
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100878 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100879}
880
881static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
882{
883 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300884 u8 old_status = *vcdev->status;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100885 struct ccw1 *ccw;
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300886 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100887
888 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
889 if (!ccw)
890 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100891
892 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100893 *vcdev->status = status;
894 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
895 ccw->flags = 0;
896 ccw->count = sizeof(status);
897 ccw->cda = (__u32)(unsigned long)vcdev->status;
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300898 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
899 /* Write failed? We assume status is unchanged. */
900 if (ret)
901 *vcdev->status = old_status;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100902 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100903}
904
905static struct virtio_config_ops virtio_ccw_config_ops = {
906 .get_features = virtio_ccw_get_features,
907 .finalize_features = virtio_ccw_finalize_features,
908 .get = virtio_ccw_get_config,
909 .set = virtio_ccw_set_config,
910 .get_status = virtio_ccw_get_status,
911 .set_status = virtio_ccw_set_status,
912 .reset = virtio_ccw_reset,
913 .find_vqs = virtio_ccw_find_vqs,
914 .del_vqs = virtio_ccw_del_vqs,
915};
916
917
918/*
919 * ccw bus driver related functions
920 */
921
922static void virtio_ccw_release_dev(struct device *_d)
923{
924 struct virtio_device *dev = container_of(_d, struct virtio_device,
925 dev);
926 struct virtio_ccw_device *vcdev = to_vc_device(dev);
927
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100928 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100929 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100930 kfree(vcdev);
931}
932
933static int irb_is_error(struct irb *irb)
934{
935 if (scsw_cstat(&irb->scsw) != 0)
936 return 1;
937 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
938 return 1;
939 if (scsw_cc(&irb->scsw) != 0)
940 return 1;
941 return 0;
942}
943
944static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
945 int index)
946{
947 struct virtio_ccw_vq_info *info;
948 unsigned long flags;
949 struct virtqueue *vq;
950
951 vq = NULL;
952 spin_lock_irqsave(&vcdev->lock, flags);
953 list_for_each_entry(info, &vcdev->virtqueues, node) {
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000954 if (info->vq->index == index) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100955 vq = info->vq;
956 break;
957 }
958 }
959 spin_unlock_irqrestore(&vcdev->lock, flags);
960 return vq;
961}
962
963static void virtio_ccw_int_handler(struct ccw_device *cdev,
964 unsigned long intparm,
965 struct irb *irb)
966{
967 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
968 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
969 int i;
970 struct virtqueue *vq;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100971
Heinz Graalfs2e021042014-02-27 14:34:35 +0100972 if (!vcdev)
973 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100974 /* Check if it's a notification from the host. */
975 if ((intparm == 0) &&
976 (scsw_stctl(&irb->scsw) ==
977 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
978 /* OK */
979 }
Cornelia Huck19e47352013-06-04 11:04:47 +0200980 if (irb_is_error(irb)) {
981 /* Command reject? */
982 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
983 (irb->ecw[0] & SNS0_CMD_REJECT))
984 vcdev->err = -EOPNOTSUPP;
985 else
986 /* Map everything else to -EIO. */
987 vcdev->err = -EIO;
988 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100989 if (vcdev->curr_io & activity) {
990 switch (activity) {
991 case VIRTIO_CCW_DOING_READ_FEAT:
992 case VIRTIO_CCW_DOING_WRITE_FEAT:
993 case VIRTIO_CCW_DOING_READ_CONFIG:
994 case VIRTIO_CCW_DOING_WRITE_CONFIG:
995 case VIRTIO_CCW_DOING_WRITE_STATUS:
996 case VIRTIO_CCW_DOING_SET_VQ:
997 case VIRTIO_CCW_DOING_SET_IND:
998 case VIRTIO_CCW_DOING_SET_CONF_IND:
999 case VIRTIO_CCW_DOING_RESET:
1000 case VIRTIO_CCW_DOING_READ_VQ_CONF:
Cornelia Huck96b14532013-02-06 10:23:39 +01001001 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
Thomas Huth6bb2c832014-10-07 16:39:50 +02001002 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
Cornelia Huck7e64e052012-12-14 17:02:18 +01001003 vcdev->curr_io &= ~activity;
1004 wake_up(&vcdev->wait_q);
1005 break;
1006 default:
1007 /* don't know what to do... */
1008 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
1009 activity);
1010 WARN_ON(1);
1011 break;
1012 }
1013 }
1014 for_each_set_bit(i, &vcdev->indicators,
1015 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1016 /* The bit clear must happen before the vring kick. */
1017 clear_bit(i, &vcdev->indicators);
1018 barrier();
1019 vq = virtio_ccw_vq_by_ind(vcdev, i);
1020 vring_interrupt(0, vq);
1021 }
1022 if (test_bit(0, &vcdev->indicators2)) {
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +10301023 virtio_config_changed(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001024 clear_bit(0, &vcdev->indicators2);
1025 }
1026}
1027
1028/*
1029 * We usually want to autoonline all devices, but give the admin
1030 * a way to exempt devices from this.
1031 */
1032#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1033 (8*sizeof(long)))
1034static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1035
1036static char *no_auto = "";
1037
1038module_param(no_auto, charp, 0444);
1039MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1040
1041static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1042{
1043 struct ccw_dev_id id;
1044
1045 ccw_device_get_id(cdev, &id);
1046 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1047 return 0;
1048 return 1;
1049}
1050
1051static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1052{
1053 struct ccw_device *cdev = data;
1054 int ret;
1055
1056 ret = ccw_device_set_online(cdev);
1057 if (ret)
1058 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1059}
1060
1061static int virtio_ccw_probe(struct ccw_device *cdev)
1062{
1063 cdev->handler = virtio_ccw_int_handler;
1064
1065 if (virtio_ccw_check_autoonline(cdev))
1066 async_schedule(virtio_ccw_auto_online, cdev);
1067 return 0;
1068}
1069
Heinz Graalfs2e021042014-02-27 14:34:35 +01001070static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1071{
1072 unsigned long flags;
1073 struct virtio_ccw_device *vcdev;
1074
1075 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1076 vcdev = dev_get_drvdata(&cdev->dev);
Heinz Graalfs79629b22014-03-05 15:23:54 +01001077 if (!vcdev || vcdev->going_away) {
Heinz Graalfs2e021042014-02-27 14:34:35 +01001078 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1079 return NULL;
1080 }
Heinz Graalfs79629b22014-03-05 15:23:54 +01001081 vcdev->going_away = true;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001082 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1083 return vcdev;
1084}
1085
Cornelia Huck7e64e052012-12-14 17:02:18 +01001086static void virtio_ccw_remove(struct ccw_device *cdev)
1087{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001088 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001089 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001090
Heinz Graalfse75279c2014-04-28 11:24:05 +09301091 if (vcdev && cdev->online) {
1092 if (vcdev->device_lost)
1093 virtio_break_device(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001094 unregister_virtio_device(&vcdev->vdev);
Heinz Graalfse75279c2014-04-28 11:24:05 +09301095 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1096 dev_set_drvdata(&cdev->dev, NULL);
1097 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1098 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001099 cdev->handler = NULL;
1100}
1101
1102static int virtio_ccw_offline(struct ccw_device *cdev)
1103{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001104 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001105 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001106
Heinz Graalfse75279c2014-04-28 11:24:05 +09301107 if (!vcdev)
1108 return 0;
1109 if (vcdev->device_lost)
1110 virtio_break_device(&vcdev->vdev);
1111 unregister_virtio_device(&vcdev->vdev);
1112 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1113 dev_set_drvdata(&cdev->dev, NULL);
1114 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001115 return 0;
1116}
1117
Thomas Huth6bb2c832014-10-07 16:39:50 +02001118static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1119{
1120 struct virtio_rev_info *rev;
1121 struct ccw1 *ccw;
1122 int ret;
1123
1124 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1125 if (!ccw)
1126 return -ENOMEM;
1127 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1128 if (!rev) {
1129 kfree(ccw);
1130 return -ENOMEM;
1131 }
1132
1133 /* Set transport revision */
1134 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1135 ccw->flags = 0;
1136 ccw->count = sizeof(*rev);
1137 ccw->cda = (__u32)(unsigned long)rev;
1138
1139 vcdev->revision = VIRTIO_CCW_REV_MAX;
1140 do {
1141 rev->revision = vcdev->revision;
1142 /* none of our supported revisions carry payload */
1143 rev->length = 0;
1144 ret = ccw_io_helper(vcdev, ccw,
1145 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1146 if (ret == -EOPNOTSUPP) {
1147 if (vcdev->revision == 0)
1148 /*
1149 * The host device does not support setting
1150 * the revision: let's operate it in legacy
1151 * mode.
1152 */
1153 ret = 0;
1154 else
1155 vcdev->revision--;
1156 }
1157 } while (ret == -EOPNOTSUPP);
1158
1159 kfree(ccw);
1160 kfree(rev);
1161 return ret;
1162}
Cornelia Huck7e64e052012-12-14 17:02:18 +01001163
Cornelia Huck7e64e052012-12-14 17:02:18 +01001164static int virtio_ccw_online(struct ccw_device *cdev)
1165{
1166 int ret;
1167 struct virtio_ccw_device *vcdev;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001168 unsigned long flags;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001169
1170 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1171 if (!vcdev) {
1172 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1173 ret = -ENOMEM;
1174 goto out_free;
1175 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001176 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1177 GFP_DMA | GFP_KERNEL);
1178 if (!vcdev->config_block) {
1179 ret = -ENOMEM;
1180 goto out_free;
1181 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001182 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1183 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +01001184 ret = -ENOMEM;
1185 goto out_free;
1186 }
1187
Cornelia Huck96b14532013-02-06 10:23:39 +01001188 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1189
Cornelia Huck7e64e052012-12-14 17:02:18 +01001190 vcdev->vdev.dev.parent = &cdev->dev;
1191 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1192 vcdev->vdev.config = &virtio_ccw_config_ops;
1193 vcdev->cdev = cdev;
1194 init_waitqueue_head(&vcdev->wait_q);
1195 INIT_LIST_HEAD(&vcdev->virtqueues);
1196 spin_lock_init(&vcdev->lock);
1197
Heinz Graalfs2e021042014-02-27 14:34:35 +01001198 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001199 dev_set_drvdata(&cdev->dev, vcdev);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001200 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001201 vcdev->vdev.id.vendor = cdev->id.cu_type;
1202 vcdev->vdev.id.device = cdev->id.cu_model;
Thomas Huth6bb2c832014-10-07 16:39:50 +02001203
Michael S. Tsirkince154082014-12-04 18:59:50 +02001204 if (virtio_device_is_legacy_only(vcdev->vdev.id)) {
1205 vcdev->revision = 0;
1206 } else {
1207 ret = virtio_ccw_set_transport_rev(vcdev);
1208 if (ret)
1209 goto out_free;
1210 }
Thomas Huth6bb2c832014-10-07 16:39:50 +02001211
Cornelia Huck7e64e052012-12-14 17:02:18 +01001212 ret = register_virtio_device(&vcdev->vdev);
1213 if (ret) {
1214 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1215 ret);
1216 goto out_put;
1217 }
1218 return 0;
1219out_put:
Heinz Graalfs2e021042014-02-27 14:34:35 +01001220 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001221 dev_set_drvdata(&cdev->dev, NULL);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001222 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001223 put_device(&vcdev->vdev.dev);
1224 return ret;
1225out_free:
1226 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001227 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001228 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001229 }
1230 kfree(vcdev);
1231 return ret;
1232}
1233
1234static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1235{
Heinz Graalfse75279c2014-04-28 11:24:05 +09301236 int rc;
1237 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1238
1239 /*
1240 * Make sure vcdev is set
1241 * i.e. set_offline/remove callback not already running
1242 */
1243 if (!vcdev)
1244 return NOTIFY_DONE;
1245
1246 switch (event) {
1247 case CIO_GONE:
1248 vcdev->device_lost = true;
1249 rc = NOTIFY_DONE;
1250 break;
1251 default:
1252 rc = NOTIFY_DONE;
1253 break;
1254 }
1255 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001256}
1257
1258static struct ccw_device_id virtio_ids[] = {
1259 { CCW_DEVICE(0x3832, 0) },
1260 {},
1261};
1262MODULE_DEVICE_TABLE(ccw, virtio_ids);
1263
1264static struct ccw_driver virtio_ccw_driver = {
1265 .driver = {
1266 .owner = THIS_MODULE,
1267 .name = "virtio_ccw",
1268 },
1269 .ids = virtio_ids,
1270 .probe = virtio_ccw_probe,
1271 .remove = virtio_ccw_remove,
1272 .set_offline = virtio_ccw_offline,
1273 .set_online = virtio_ccw_online,
1274 .notify = virtio_ccw_cio_notify,
Linus Torvalds89f88332013-02-24 13:07:18 -08001275 .int_class = IRQIO_VIR,
Cornelia Huck7e64e052012-12-14 17:02:18 +01001276};
1277
1278static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1279 int max_digit, int max_val)
1280{
1281 int diff;
1282
1283 diff = 0;
1284 *val = 0;
1285
1286 while (diff <= max_digit) {
1287 int value = hex_to_bin(**cp);
1288
1289 if (value < 0)
1290 break;
1291 *val = *val * 16 + value;
1292 (*cp)++;
1293 diff++;
1294 }
1295
1296 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1297 return 1;
1298
1299 return 0;
1300}
1301
1302static int __init parse_busid(char *str, unsigned int *cssid,
1303 unsigned int *ssid, unsigned int *devno)
1304{
1305 char *str_work;
1306 int rc, ret;
1307
1308 rc = 1;
1309
1310 if (*str == '\0')
1311 goto out;
1312
1313 str_work = str;
1314 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1315 if (ret || (str_work[0] != '.'))
1316 goto out;
1317 str_work++;
1318 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1319 if (ret || (str_work[0] != '.'))
1320 goto out;
1321 str_work++;
1322 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1323 if (ret || (str_work[0] != '\0'))
1324 goto out;
1325
1326 rc = 0;
1327out:
1328 return rc;
1329}
1330
1331static void __init no_auto_parse(void)
1332{
1333 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1334 char *parm, *str;
1335 int rc;
1336
1337 str = no_auto;
1338 while ((parm = strsep(&str, ","))) {
1339 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1340 &from_ssid, &from);
1341 if (rc)
1342 continue;
1343 if (parm != NULL) {
1344 rc = parse_busid(parm, &to_cssid,
1345 &to_ssid, &to);
1346 if ((from_ssid > to_ssid) ||
1347 ((from_ssid == to_ssid) && (from > to)))
1348 rc = -EINVAL;
1349 } else {
1350 to_cssid = from_cssid;
1351 to_ssid = from_ssid;
1352 to = from;
1353 }
1354 if (rc)
1355 continue;
1356 while ((from_ssid < to_ssid) ||
1357 ((from_ssid == to_ssid) && (from <= to))) {
1358 set_bit(from, devs_no_auto[from_ssid]);
1359 from++;
1360 if (from > __MAX_SUBCHANNEL) {
1361 from_ssid++;
1362 from = 0;
1363 }
1364 }
1365 }
1366}
1367
1368static int __init virtio_ccw_init(void)
1369{
1370 /* parse no_auto string before we do anything further */
1371 no_auto_parse();
1372 return ccw_driver_register(&virtio_ccw_driver);
1373}
1374module_init(virtio_ccw_init);
1375
1376static void __exit virtio_ccw_exit(void)
1377{
Cornelia Huck96b14532013-02-06 10:23:39 +01001378 int i;
1379
Cornelia Huck7e64e052012-12-14 17:02:18 +01001380 ccw_driver_unregister(&virtio_ccw_driver);
Cornelia Huck96b14532013-02-06 10:23:39 +01001381 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1382 destroy_airq_info(airq_areas[i]);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001383}
1384module_exit(virtio_ccw_exit);