blob: 6cbe6ef3c889d14841e2c73aa848fa9f563b68ec [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;
58 wait_queue_head_t wait_q;
59 spinlock_t lock;
60 struct list_head virtqueues;
61 unsigned long indicators;
62 unsigned long indicators2;
63 struct vq_config_block *config_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010064 bool is_thinint;
Heinz Graalfs79629b22014-03-05 15:23:54 +010065 bool going_away;
Heinz Graalfse75279c2014-04-28 11:24:05 +093066 bool device_lost;
Cornelia Huck96b14532013-02-06 10:23:39 +010067 void *airq_info;
Cornelia Huck7e64e052012-12-14 17:02:18 +010068};
69
70struct vq_info_block {
71 __u64 queue;
72 __u32 align;
73 __u16 index;
74 __u16 num;
75} __packed;
76
77struct virtio_feature_desc {
78 __u32 features;
79 __u8 index;
80} __packed;
81
Cornelia Huck96b14532013-02-06 10:23:39 +010082struct virtio_thinint_area {
83 unsigned long summary_indicator;
84 unsigned long indicator;
85 u64 bit_nr;
86 u8 isc;
87} __packed;
88
Cornelia Huck7e64e052012-12-14 17:02:18 +010089struct virtio_ccw_vq_info {
90 struct virtqueue *vq;
91 int num;
92 void *queue;
93 struct vq_info_block *info_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010094 int bit_nr;
Cornelia Huck7e64e052012-12-14 17:02:18 +010095 struct list_head node;
Michael S. Tsirkin07e16932013-02-28 12:33:16 +010096 long cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +010097};
98
Cornelia Huck96b14532013-02-06 10:23:39 +010099#define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
100
101#define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
102#define MAX_AIRQ_AREAS 20
103
104static int virtio_ccw_use_airq = 1;
105
106struct airq_info {
107 rwlock_t lock;
108 u8 summary_indicator;
109 struct airq_struct airq;
110 struct airq_iv *aiv;
111};
112static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
113
Cornelia Huck7e64e052012-12-14 17:02:18 +0100114#define CCW_CMD_SET_VQ 0x13
115#define CCW_CMD_VDEV_RESET 0x33
116#define CCW_CMD_SET_IND 0x43
117#define CCW_CMD_SET_CONF_IND 0x53
118#define CCW_CMD_READ_FEAT 0x12
119#define CCW_CMD_WRITE_FEAT 0x11
120#define CCW_CMD_READ_CONF 0x22
121#define CCW_CMD_WRITE_CONF 0x21
122#define CCW_CMD_WRITE_STATUS 0x31
123#define CCW_CMD_READ_VQ_CONF 0x32
Cornelia Huck96b14532013-02-06 10:23:39 +0100124#define CCW_CMD_SET_IND_ADAPTER 0x73
Cornelia Huck7e64e052012-12-14 17:02:18 +0100125
126#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
127#define VIRTIO_CCW_DOING_RESET 0x00040000
128#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
129#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
130#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
131#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
132#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
133#define VIRTIO_CCW_DOING_SET_IND 0x01000000
134#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
135#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
Cornelia Huck96b14532013-02-06 10:23:39 +0100136#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
Cornelia Huck7e64e052012-12-14 17:02:18 +0100137#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
138
139static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
140{
141 return container_of(vdev, struct virtio_ccw_device, vdev);
142}
143
Cornelia Huck96b14532013-02-06 10:23:39 +0100144static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
145{
146 unsigned long i, flags;
147
148 write_lock_irqsave(&info->lock, flags);
149 for (i = 0; i < airq_iv_end(info->aiv); i++) {
150 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
151 airq_iv_free_bit(info->aiv, i);
152 airq_iv_set_ptr(info->aiv, i, 0);
153 break;
154 }
155 }
156 write_unlock_irqrestore(&info->lock, flags);
157}
158
159static void virtio_airq_handler(struct airq_struct *airq)
160{
161 struct airq_info *info = container_of(airq, struct airq_info, airq);
162 unsigned long ai;
163
164 inc_irq_stat(IRQIO_VAI);
165 read_lock(&info->lock);
166 /* Walk through indicators field, summary indicator active. */
167 for (ai = 0;;) {
168 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
169 if (ai == -1UL)
170 break;
171 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
172 }
173 info->summary_indicator = 0;
174 smp_wmb();
175 /* Walk through indicators field, summary indicator not active. */
176 for (ai = 0;;) {
177 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
178 if (ai == -1UL)
179 break;
180 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
181 }
182 read_unlock(&info->lock);
183}
184
185static struct airq_info *new_airq_info(void)
186{
187 struct airq_info *info;
188 int rc;
189
190 info = kzalloc(sizeof(*info), GFP_KERNEL);
191 if (!info)
192 return NULL;
193 rwlock_init(&info->lock);
194 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
195 if (!info->aiv) {
196 kfree(info);
197 return NULL;
198 }
199 info->airq.handler = virtio_airq_handler;
200 info->airq.lsi_ptr = &info->summary_indicator;
201 info->airq.lsi_mask = 0xff;
202 info->airq.isc = VIRTIO_AIRQ_ISC;
203 rc = register_adapter_interrupt(&info->airq);
204 if (rc) {
205 airq_iv_release(info->aiv);
206 kfree(info);
207 return NULL;
208 }
209 return info;
210}
211
212static void destroy_airq_info(struct airq_info *info)
213{
214 if (!info)
215 return;
216
217 unregister_adapter_interrupt(&info->airq);
218 airq_iv_release(info->aiv);
219 kfree(info);
220}
221
222static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
223 u64 *first, void **airq_info)
224{
225 int i, j;
226 struct airq_info *info;
227 unsigned long indicator_addr = 0;
228 unsigned long bit, flags;
229
230 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
231 if (!airq_areas[i])
232 airq_areas[i] = new_airq_info();
233 info = airq_areas[i];
234 if (!info)
235 return 0;
236 write_lock_irqsave(&info->lock, flags);
237 bit = airq_iv_alloc(info->aiv, nvqs);
238 if (bit == -1UL) {
239 /* Not enough vacancies. */
240 write_unlock_irqrestore(&info->lock, flags);
241 continue;
242 }
243 *first = bit;
244 *airq_info = info;
245 indicator_addr = (unsigned long)info->aiv->vector;
246 for (j = 0; j < nvqs; j++) {
247 airq_iv_set_ptr(info->aiv, bit + j,
248 (unsigned long)vqs[j]);
249 }
250 write_unlock_irqrestore(&info->lock, flags);
251 }
252 return indicator_addr;
253}
254
255static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
256{
257 struct virtio_ccw_vq_info *info;
258
259 list_for_each_entry(info, &vcdev->virtqueues, node)
260 drop_airq_indicator(info->vq, vcdev->airq_info);
261}
262
Cornelia Huck7e64e052012-12-14 17:02:18 +0100263static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
264{
265 unsigned long flags;
266 __u32 ret;
267
268 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
269 if (vcdev->err)
270 ret = 0;
271 else
272 ret = vcdev->curr_io & flag;
273 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
274 return ret;
275}
276
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100277static int ccw_io_helper(struct virtio_ccw_device *vcdev,
278 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100279{
280 int ret;
281 unsigned long flags;
282 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
283
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100284 do {
285 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
286 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
Cornelia Huck99437a22013-04-04 10:25:06 +0200287 if (!ret) {
288 if (!vcdev->curr_io)
289 vcdev->err = 0;
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100290 vcdev->curr_io |= flag;
Cornelia Huck99437a22013-04-04 10:25:06 +0200291 }
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100292 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
293 cpu_relax();
294 } while (ret == -EBUSY);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100295 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
296 return ret ? ret : vcdev->err;
297}
298
Cornelia Huck96b14532013-02-06 10:23:39 +0100299static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
300 struct ccw1 *ccw)
301{
302 int ret;
303 unsigned long *indicatorp = NULL;
304 struct virtio_thinint_area *thinint_area = NULL;
305 struct airq_info *airq_info = vcdev->airq_info;
306
307 if (vcdev->is_thinint) {
308 thinint_area = kzalloc(sizeof(*thinint_area),
309 GFP_DMA | GFP_KERNEL);
310 if (!thinint_area)
311 return;
312 thinint_area->summary_indicator =
313 (unsigned long) &airq_info->summary_indicator;
314 thinint_area->isc = VIRTIO_AIRQ_ISC;
315 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
316 ccw->count = sizeof(*thinint_area);
317 ccw->cda = (__u32)(unsigned long) thinint_area;
318 } else {
319 indicatorp = kmalloc(sizeof(&vcdev->indicators),
320 GFP_DMA | GFP_KERNEL);
321 if (!indicatorp)
322 return;
323 *indicatorp = 0;
324 ccw->cmd_code = CCW_CMD_SET_IND;
325 ccw->count = sizeof(vcdev->indicators);
326 ccw->cda = (__u32)(unsigned long) indicatorp;
327 }
328 /* Deregister indicators from host. */
329 vcdev->indicators = 0;
330 ccw->flags = 0;
331 ret = ccw_io_helper(vcdev, ccw,
332 vcdev->is_thinint ?
333 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
334 VIRTIO_CCW_DOING_SET_IND);
335 if (ret && (ret != -ENODEV))
336 dev_info(&vcdev->cdev->dev,
337 "Failed to deregister indicators (%d)\n", ret);
338 else if (vcdev->is_thinint)
339 virtio_ccw_drop_indicators(vcdev);
340 kfree(indicatorp);
341 kfree(thinint_area);
342}
343
Cornelia Huck7e64e052012-12-14 17:02:18 +0100344static inline long do_kvm_notify(struct subchannel_id schid,
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100345 unsigned long queue_index,
346 long cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100347{
348 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
349 register struct subchannel_id __schid asm("2") = schid;
350 register unsigned long __index asm("3") = queue_index;
351 register long __rc asm("2");
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100352 register long __cookie asm("4") = cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100353
354 asm volatile ("diag 2,4,0x500\n"
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100355 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
356 "d"(__cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100357 : "memory", "cc");
358 return __rc;
359}
360
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030361static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100362{
363 struct virtio_ccw_vq_info *info = vq->priv;
364 struct virtio_ccw_device *vcdev;
365 struct subchannel_id schid;
366
367 vcdev = to_vc_device(info->vq->vdev);
368 ccw_device_get_schid(vcdev->cdev, &schid);
Linus Torvalds01227a82013-05-05 14:47:31 -0700369 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030370 if (info->cookie < 0)
371 return false;
372 return true;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100373}
374
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100375static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
376 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100377{
378 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100379 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
380 ccw->flags = 0;
381 ccw->count = sizeof(struct vq_config_block);
382 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
383 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100384 return vcdev->config_block->num;
385}
386
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100387static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100388{
389 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
390 struct virtio_ccw_vq_info *info = vq->priv;
391 unsigned long flags;
392 unsigned long size;
393 int ret;
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000394 unsigned int index = vq->index;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100395
396 /* Remove from our list. */
397 spin_lock_irqsave(&vcdev->lock, flags);
398 list_del(&info->node);
399 spin_unlock_irqrestore(&vcdev->lock, flags);
400
401 /* Release from host. */
402 info->info_block->queue = 0;
403 info->info_block->align = 0;
404 info->info_block->index = index;
405 info->info_block->num = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100406 ccw->cmd_code = CCW_CMD_SET_VQ;
407 ccw->flags = 0;
408 ccw->count = sizeof(*info->info_block);
409 ccw->cda = (__u32)(unsigned long)(info->info_block);
410 ret = ccw_io_helper(vcdev, ccw,
411 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100412 /*
413 * -ENODEV isn't considered an error: The device is gone anyway.
414 * This may happen on device detach.
415 */
416 if (ret && (ret != -ENODEV))
417 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
418 ret, index);
419
420 vring_del_virtqueue(vq);
421 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
422 free_pages_exact(info->queue, size);
423 kfree(info->info_block);
424 kfree(info);
425}
426
427static void virtio_ccw_del_vqs(struct virtio_device *vdev)
428{
429 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100430 struct ccw1 *ccw;
Cornelia Huck96b14532013-02-06 10:23:39 +0100431 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100432
433 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
434 if (!ccw)
435 return;
436
Cornelia Huck96b14532013-02-06 10:23:39 +0100437 virtio_ccw_drop_indicator(vcdev, ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100438
439 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100440 virtio_ccw_del_vq(vq, ccw);
441
442 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100443}
444
445static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
446 int i, vq_callback_t *callback,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100447 const char *name,
448 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100449{
450 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
451 int err;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100452 struct virtqueue *vq = NULL;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100453 struct virtio_ccw_vq_info *info;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100454 unsigned long size = 0; /* silence the compiler */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100455 unsigned long flags;
456
457 /* Allocate queue. */
458 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
459 if (!info) {
460 dev_warn(&vcdev->cdev->dev, "no info\n");
461 err = -ENOMEM;
462 goto out_err;
463 }
464 info->info_block = kzalloc(sizeof(*info->info_block),
465 GFP_DMA | GFP_KERNEL);
466 if (!info->info_block) {
467 dev_warn(&vcdev->cdev->dev, "no info block\n");
468 err = -ENOMEM;
469 goto out_err;
470 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100471 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100472 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
473 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
474 if (info->queue == NULL) {
475 dev_warn(&vcdev->cdev->dev, "no queue\n");
476 err = -ENOMEM;
477 goto out_err;
478 }
479
480 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
481 true, info->queue, virtio_ccw_kvm_notify,
482 callback, name);
483 if (!vq) {
484 /* For now, we fail if we can't get the requested size. */
485 dev_warn(&vcdev->cdev->dev, "no vq\n");
486 err = -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100487 goto out_err;
488 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100489
490 /* Register it with the host. */
491 info->info_block->queue = (__u64)info->queue;
492 info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
493 info->info_block->index = i;
494 info->info_block->num = info->num;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100495 ccw->cmd_code = CCW_CMD_SET_VQ;
496 ccw->flags = 0;
497 ccw->count = sizeof(*info->info_block);
498 ccw->cda = (__u32)(unsigned long)(info->info_block);
499 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100500 if (err) {
501 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
Cornelia Huck7e64e052012-12-14 17:02:18 +0100502 goto out_err;
503 }
504
Cornelia Huckc98d3682013-01-25 15:34:16 +0100505 info->vq = vq;
506 vq->priv = info;
507
Cornelia Huck7e64e052012-12-14 17:02:18 +0100508 /* Save it to our list. */
509 spin_lock_irqsave(&vcdev->lock, flags);
510 list_add(&info->node, &vcdev->virtqueues);
511 spin_unlock_irqrestore(&vcdev->lock, flags);
512
513 return vq;
514
515out_err:
Cornelia Huckc98d3682013-01-25 15:34:16 +0100516 if (vq)
517 vring_del_virtqueue(vq);
518 if (info) {
519 if (info->queue)
520 free_pages_exact(info->queue, size);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100521 kfree(info->info_block);
Cornelia Huckc98d3682013-01-25 15:34:16 +0100522 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100523 kfree(info);
524 return ERR_PTR(err);
525}
526
Cornelia Huck96b14532013-02-06 10:23:39 +0100527static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
528 struct virtqueue *vqs[], int nvqs,
529 struct ccw1 *ccw)
530{
531 int ret;
532 struct virtio_thinint_area *thinint_area = NULL;
533 struct airq_info *info;
534
535 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
536 if (!thinint_area) {
537 ret = -ENOMEM;
538 goto out;
539 }
540 /* Try to get an indicator. */
541 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
542 &thinint_area->bit_nr,
543 &vcdev->airq_info);
544 if (!thinint_area->indicator) {
545 ret = -ENOSPC;
546 goto out;
547 }
548 info = vcdev->airq_info;
549 thinint_area->summary_indicator =
550 (unsigned long) &info->summary_indicator;
551 thinint_area->isc = VIRTIO_AIRQ_ISC;
552 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
553 ccw->flags = CCW_FLAG_SLI;
554 ccw->count = sizeof(*thinint_area);
555 ccw->cda = (__u32)(unsigned long)thinint_area;
556 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
557 if (ret) {
558 if (ret == -EOPNOTSUPP) {
559 /*
560 * The host does not support adapter interrupts
561 * for virtio-ccw, stop trying.
562 */
563 virtio_ccw_use_airq = 0;
564 pr_info("Adapter interrupts unsupported on host\n");
565 } else
566 dev_warn(&vcdev->cdev->dev,
567 "enabling adapter interrupts = %d\n", ret);
568 virtio_ccw_drop_indicators(vcdev);
569 }
570out:
571 kfree(thinint_area);
572 return ret;
573}
574
Cornelia Huck7e64e052012-12-14 17:02:18 +0100575static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
576 struct virtqueue *vqs[],
577 vq_callback_t *callbacks[],
578 const char *names[])
579{
580 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
581 unsigned long *indicatorp = NULL;
582 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100583 struct ccw1 *ccw;
584
585 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
586 if (!ccw)
587 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100588
589 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100590 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
591 ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100592 if (IS_ERR(vqs[i])) {
593 ret = PTR_ERR(vqs[i]);
594 vqs[i] = NULL;
595 goto out;
596 }
597 }
598 ret = -ENOMEM;
599 /* We need a data area under 2G to communicate. */
600 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
601 if (!indicatorp)
602 goto out;
603 *indicatorp = (unsigned long) &vcdev->indicators;
Cornelia Huck96b14532013-02-06 10:23:39 +0100604 if (vcdev->is_thinint) {
605 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
606 if (ret)
607 /* no error, just fall back to legacy interrupts */
608 vcdev->is_thinint = 0;
609 }
610 if (!vcdev->is_thinint) {
611 /* Register queue indicators with host. */
612 vcdev->indicators = 0;
613 ccw->cmd_code = CCW_CMD_SET_IND;
614 ccw->flags = 0;
615 ccw->count = sizeof(vcdev->indicators);
616 ccw->cda = (__u32)(unsigned long) indicatorp;
617 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
618 if (ret)
619 goto out;
620 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100621 /* Register indicators2 with host for config changes */
622 *indicatorp = (unsigned long) &vcdev->indicators2;
623 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100624 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
625 ccw->flags = 0;
626 ccw->count = sizeof(vcdev->indicators2);
627 ccw->cda = (__u32)(unsigned long) indicatorp;
628 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100629 if (ret)
630 goto out;
631
632 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100633 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100634 return 0;
635out:
636 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100637 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100638 virtio_ccw_del_vqs(vdev);
639 return ret;
640}
641
642static void virtio_ccw_reset(struct virtio_device *vdev)
643{
644 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100645 struct ccw1 *ccw;
646
647 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
648 if (!ccw)
649 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100650
651 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100652 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100653
654 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100655 ccw->cmd_code = CCW_CMD_VDEV_RESET;
656 ccw->flags = 0;
657 ccw->count = 0;
658 ccw->cda = 0;
659 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
660 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100661}
662
663static u32 virtio_ccw_get_features(struct virtio_device *vdev)
664{
665 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100666 struct virtio_feature_desc *features;
667 int ret, rc;
668 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100669
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100670 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
671 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100672 return 0;
673
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100674 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
675 if (!features) {
676 rc = 0;
677 goto out_free;
678 }
679 /* Read the feature bits from the host. */
680 /* TODO: Features > 32 bits */
681 features->index = 0;
682 ccw->cmd_code = CCW_CMD_READ_FEAT;
683 ccw->flags = 0;
684 ccw->count = sizeof(*features);
685 ccw->cda = (__u32)(unsigned long)features;
686 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
687 if (ret) {
688 rc = 0;
689 goto out_free;
690 }
691
692 rc = le32_to_cpu(features->features);
693
694out_free:
695 kfree(features);
696 kfree(ccw);
697 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100698}
699
700static void virtio_ccw_finalize_features(struct virtio_device *vdev)
701{
702 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100703 struct virtio_feature_desc *features;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100704 int i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100705 struct ccw1 *ccw;
706
707 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
708 if (!ccw)
709 return;
710
711 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
712 if (!features)
713 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100714
715 /* Give virtio_ring a chance to accept features. */
716 vring_transport_features(vdev);
717
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100718 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100719 i++) {
720 int highbits = i % 2 ? 32 : 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100721 features->index = i;
722 features->features = cpu_to_le32(vdev->features[i / 2]
723 >> highbits);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100724 /* Write the feature bits to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100725 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
726 ccw->flags = 0;
727 ccw->count = sizeof(*features);
728 ccw->cda = (__u32)(unsigned long)features;
729 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100730 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100731out_free:
732 kfree(features);
733 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100734}
735
736static void virtio_ccw_get_config(struct virtio_device *vdev,
737 unsigned int offset, void *buf, unsigned len)
738{
739 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
740 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100741 struct ccw1 *ccw;
742 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100743
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100744 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
745 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100746 return;
747
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100748 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
749 if (!config_area)
750 goto out_free;
751
752 /* Read the config area from the host. */
753 ccw->cmd_code = CCW_CMD_READ_CONF;
754 ccw->flags = 0;
755 ccw->count = offset + len;
756 ccw->cda = (__u32)(unsigned long)config_area;
757 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
758 if (ret)
759 goto out_free;
760
761 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
Cornelia Huck7e64e052012-12-14 17:02:18 +0100762 memcpy(buf, &vcdev->config[offset], len);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100763
764out_free:
765 kfree(config_area);
766 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100767}
768
769static void virtio_ccw_set_config(struct virtio_device *vdev,
770 unsigned int offset, const void *buf,
771 unsigned len)
772{
773 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100774 struct ccw1 *ccw;
775 void *config_area;
776
777 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
778 if (!ccw)
779 return;
780
781 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
782 if (!config_area)
783 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100784
785 memcpy(&vcdev->config[offset], buf, len);
786 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100787 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
788 ccw->cmd_code = CCW_CMD_WRITE_CONF;
789 ccw->flags = 0;
790 ccw->count = offset + len;
791 ccw->cda = (__u32)(unsigned long)config_area;
792 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
793
794out_free:
795 kfree(config_area);
796 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100797}
798
799static u8 virtio_ccw_get_status(struct virtio_device *vdev)
800{
801 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
802
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100803 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100804}
805
806static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
807{
808 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100809 struct ccw1 *ccw;
810
811 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
812 if (!ccw)
813 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100814
815 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100816 *vcdev->status = status;
817 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
818 ccw->flags = 0;
819 ccw->count = sizeof(status);
820 ccw->cda = (__u32)(unsigned long)vcdev->status;
821 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
822 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100823}
824
825static struct virtio_config_ops virtio_ccw_config_ops = {
826 .get_features = virtio_ccw_get_features,
827 .finalize_features = virtio_ccw_finalize_features,
828 .get = virtio_ccw_get_config,
829 .set = virtio_ccw_set_config,
830 .get_status = virtio_ccw_get_status,
831 .set_status = virtio_ccw_set_status,
832 .reset = virtio_ccw_reset,
833 .find_vqs = virtio_ccw_find_vqs,
834 .del_vqs = virtio_ccw_del_vqs,
835};
836
837
838/*
839 * ccw bus driver related functions
840 */
841
842static void virtio_ccw_release_dev(struct device *_d)
843{
844 struct virtio_device *dev = container_of(_d, struct virtio_device,
845 dev);
846 struct virtio_ccw_device *vcdev = to_vc_device(dev);
847
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100848 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100849 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100850 kfree(vcdev);
851}
852
853static int irb_is_error(struct irb *irb)
854{
855 if (scsw_cstat(&irb->scsw) != 0)
856 return 1;
857 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
858 return 1;
859 if (scsw_cc(&irb->scsw) != 0)
860 return 1;
861 return 0;
862}
863
864static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
865 int index)
866{
867 struct virtio_ccw_vq_info *info;
868 unsigned long flags;
869 struct virtqueue *vq;
870
871 vq = NULL;
872 spin_lock_irqsave(&vcdev->lock, flags);
873 list_for_each_entry(info, &vcdev->virtqueues, node) {
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000874 if (info->vq->index == index) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100875 vq = info->vq;
876 break;
877 }
878 }
879 spin_unlock_irqrestore(&vcdev->lock, flags);
880 return vq;
881}
882
883static void virtio_ccw_int_handler(struct ccw_device *cdev,
884 unsigned long intparm,
885 struct irb *irb)
886{
887 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
888 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
889 int i;
890 struct virtqueue *vq;
891 struct virtio_driver *drv;
892
Heinz Graalfs2e021042014-02-27 14:34:35 +0100893 if (!vcdev)
894 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100895 /* Check if it's a notification from the host. */
896 if ((intparm == 0) &&
897 (scsw_stctl(&irb->scsw) ==
898 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
899 /* OK */
900 }
Cornelia Huck19e47352013-06-04 11:04:47 +0200901 if (irb_is_error(irb)) {
902 /* Command reject? */
903 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
904 (irb->ecw[0] & SNS0_CMD_REJECT))
905 vcdev->err = -EOPNOTSUPP;
906 else
907 /* Map everything else to -EIO. */
908 vcdev->err = -EIO;
909 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100910 if (vcdev->curr_io & activity) {
911 switch (activity) {
912 case VIRTIO_CCW_DOING_READ_FEAT:
913 case VIRTIO_CCW_DOING_WRITE_FEAT:
914 case VIRTIO_CCW_DOING_READ_CONFIG:
915 case VIRTIO_CCW_DOING_WRITE_CONFIG:
916 case VIRTIO_CCW_DOING_WRITE_STATUS:
917 case VIRTIO_CCW_DOING_SET_VQ:
918 case VIRTIO_CCW_DOING_SET_IND:
919 case VIRTIO_CCW_DOING_SET_CONF_IND:
920 case VIRTIO_CCW_DOING_RESET:
921 case VIRTIO_CCW_DOING_READ_VQ_CONF:
Cornelia Huck96b14532013-02-06 10:23:39 +0100922 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
Cornelia Huck7e64e052012-12-14 17:02:18 +0100923 vcdev->curr_io &= ~activity;
924 wake_up(&vcdev->wait_q);
925 break;
926 default:
927 /* don't know what to do... */
928 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
929 activity);
930 WARN_ON(1);
931 break;
932 }
933 }
934 for_each_set_bit(i, &vcdev->indicators,
935 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
936 /* The bit clear must happen before the vring kick. */
937 clear_bit(i, &vcdev->indicators);
938 barrier();
939 vq = virtio_ccw_vq_by_ind(vcdev, i);
940 vring_interrupt(0, vq);
941 }
942 if (test_bit(0, &vcdev->indicators2)) {
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030943 virtio_config_changed(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100944 clear_bit(0, &vcdev->indicators2);
945 }
946}
947
948/*
949 * We usually want to autoonline all devices, but give the admin
950 * a way to exempt devices from this.
951 */
952#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
953 (8*sizeof(long)))
954static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
955
956static char *no_auto = "";
957
958module_param(no_auto, charp, 0444);
959MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
960
961static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
962{
963 struct ccw_dev_id id;
964
965 ccw_device_get_id(cdev, &id);
966 if (test_bit(id.devno, devs_no_auto[id.ssid]))
967 return 0;
968 return 1;
969}
970
971static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
972{
973 struct ccw_device *cdev = data;
974 int ret;
975
976 ret = ccw_device_set_online(cdev);
977 if (ret)
978 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
979}
980
981static int virtio_ccw_probe(struct ccw_device *cdev)
982{
983 cdev->handler = virtio_ccw_int_handler;
984
985 if (virtio_ccw_check_autoonline(cdev))
986 async_schedule(virtio_ccw_auto_online, cdev);
987 return 0;
988}
989
Heinz Graalfs2e021042014-02-27 14:34:35 +0100990static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
991{
992 unsigned long flags;
993 struct virtio_ccw_device *vcdev;
994
995 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
996 vcdev = dev_get_drvdata(&cdev->dev);
Heinz Graalfs79629b22014-03-05 15:23:54 +0100997 if (!vcdev || vcdev->going_away) {
Heinz Graalfs2e021042014-02-27 14:34:35 +0100998 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
999 return NULL;
1000 }
Heinz Graalfs79629b22014-03-05 15:23:54 +01001001 vcdev->going_away = true;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001002 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1003 return vcdev;
1004}
1005
Cornelia Huck7e64e052012-12-14 17:02:18 +01001006static void virtio_ccw_remove(struct ccw_device *cdev)
1007{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001008 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001009 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001010
Heinz Graalfse75279c2014-04-28 11:24:05 +09301011 if (vcdev && cdev->online) {
1012 if (vcdev->device_lost)
1013 virtio_break_device(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001014 unregister_virtio_device(&vcdev->vdev);
Heinz Graalfse75279c2014-04-28 11:24:05 +09301015 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1016 dev_set_drvdata(&cdev->dev, NULL);
1017 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1018 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001019 cdev->handler = NULL;
1020}
1021
1022static int virtio_ccw_offline(struct ccw_device *cdev)
1023{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001024 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001025 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001026
Heinz Graalfse75279c2014-04-28 11:24:05 +09301027 if (!vcdev)
1028 return 0;
1029 if (vcdev->device_lost)
1030 virtio_break_device(&vcdev->vdev);
1031 unregister_virtio_device(&vcdev->vdev);
1032 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1033 dev_set_drvdata(&cdev->dev, NULL);
1034 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001035 return 0;
1036}
1037
1038
Cornelia Huck7e64e052012-12-14 17:02:18 +01001039static int virtio_ccw_online(struct ccw_device *cdev)
1040{
1041 int ret;
1042 struct virtio_ccw_device *vcdev;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001043 unsigned long flags;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001044
1045 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1046 if (!vcdev) {
1047 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1048 ret = -ENOMEM;
1049 goto out_free;
1050 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001051 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1052 GFP_DMA | GFP_KERNEL);
1053 if (!vcdev->config_block) {
1054 ret = -ENOMEM;
1055 goto out_free;
1056 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001057 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1058 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +01001059 ret = -ENOMEM;
1060 goto out_free;
1061 }
1062
Cornelia Huck96b14532013-02-06 10:23:39 +01001063 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1064
Cornelia Huck7e64e052012-12-14 17:02:18 +01001065 vcdev->vdev.dev.parent = &cdev->dev;
1066 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1067 vcdev->vdev.config = &virtio_ccw_config_ops;
1068 vcdev->cdev = cdev;
1069 init_waitqueue_head(&vcdev->wait_q);
1070 INIT_LIST_HEAD(&vcdev->virtqueues);
1071 spin_lock_init(&vcdev->lock);
1072
Heinz Graalfs2e021042014-02-27 14:34:35 +01001073 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001074 dev_set_drvdata(&cdev->dev, vcdev);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001075 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001076 vcdev->vdev.id.vendor = cdev->id.cu_type;
1077 vcdev->vdev.id.device = cdev->id.cu_model;
1078 ret = register_virtio_device(&vcdev->vdev);
1079 if (ret) {
1080 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1081 ret);
1082 goto out_put;
1083 }
1084 return 0;
1085out_put:
Heinz Graalfs2e021042014-02-27 14:34:35 +01001086 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001087 dev_set_drvdata(&cdev->dev, NULL);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001088 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001089 put_device(&vcdev->vdev.dev);
1090 return ret;
1091out_free:
1092 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001093 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001094 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001095 }
1096 kfree(vcdev);
1097 return ret;
1098}
1099
1100static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1101{
Heinz Graalfse75279c2014-04-28 11:24:05 +09301102 int rc;
1103 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1104
1105 /*
1106 * Make sure vcdev is set
1107 * i.e. set_offline/remove callback not already running
1108 */
1109 if (!vcdev)
1110 return NOTIFY_DONE;
1111
1112 switch (event) {
1113 case CIO_GONE:
1114 vcdev->device_lost = true;
1115 rc = NOTIFY_DONE;
1116 break;
1117 default:
1118 rc = NOTIFY_DONE;
1119 break;
1120 }
1121 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001122}
1123
1124static struct ccw_device_id virtio_ids[] = {
1125 { CCW_DEVICE(0x3832, 0) },
1126 {},
1127};
1128MODULE_DEVICE_TABLE(ccw, virtio_ids);
1129
1130static struct ccw_driver virtio_ccw_driver = {
1131 .driver = {
1132 .owner = THIS_MODULE,
1133 .name = "virtio_ccw",
1134 },
1135 .ids = virtio_ids,
1136 .probe = virtio_ccw_probe,
1137 .remove = virtio_ccw_remove,
1138 .set_offline = virtio_ccw_offline,
1139 .set_online = virtio_ccw_online,
1140 .notify = virtio_ccw_cio_notify,
Linus Torvalds89f88332013-02-24 13:07:18 -08001141 .int_class = IRQIO_VIR,
Cornelia Huck7e64e052012-12-14 17:02:18 +01001142};
1143
1144static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1145 int max_digit, int max_val)
1146{
1147 int diff;
1148
1149 diff = 0;
1150 *val = 0;
1151
1152 while (diff <= max_digit) {
1153 int value = hex_to_bin(**cp);
1154
1155 if (value < 0)
1156 break;
1157 *val = *val * 16 + value;
1158 (*cp)++;
1159 diff++;
1160 }
1161
1162 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1163 return 1;
1164
1165 return 0;
1166}
1167
1168static int __init parse_busid(char *str, unsigned int *cssid,
1169 unsigned int *ssid, unsigned int *devno)
1170{
1171 char *str_work;
1172 int rc, ret;
1173
1174 rc = 1;
1175
1176 if (*str == '\0')
1177 goto out;
1178
1179 str_work = str;
1180 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1181 if (ret || (str_work[0] != '.'))
1182 goto out;
1183 str_work++;
1184 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1185 if (ret || (str_work[0] != '.'))
1186 goto out;
1187 str_work++;
1188 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1189 if (ret || (str_work[0] != '\0'))
1190 goto out;
1191
1192 rc = 0;
1193out:
1194 return rc;
1195}
1196
1197static void __init no_auto_parse(void)
1198{
1199 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1200 char *parm, *str;
1201 int rc;
1202
1203 str = no_auto;
1204 while ((parm = strsep(&str, ","))) {
1205 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1206 &from_ssid, &from);
1207 if (rc)
1208 continue;
1209 if (parm != NULL) {
1210 rc = parse_busid(parm, &to_cssid,
1211 &to_ssid, &to);
1212 if ((from_ssid > to_ssid) ||
1213 ((from_ssid == to_ssid) && (from > to)))
1214 rc = -EINVAL;
1215 } else {
1216 to_cssid = from_cssid;
1217 to_ssid = from_ssid;
1218 to = from;
1219 }
1220 if (rc)
1221 continue;
1222 while ((from_ssid < to_ssid) ||
1223 ((from_ssid == to_ssid) && (from <= to))) {
1224 set_bit(from, devs_no_auto[from_ssid]);
1225 from++;
1226 if (from > __MAX_SUBCHANNEL) {
1227 from_ssid++;
1228 from = 0;
1229 }
1230 }
1231 }
1232}
1233
1234static int __init virtio_ccw_init(void)
1235{
1236 /* parse no_auto string before we do anything further */
1237 no_auto_parse();
1238 return ccw_driver_register(&virtio_ccw_driver);
1239}
1240module_init(virtio_ccw_init);
1241
1242static void __exit virtio_ccw_exit(void)
1243{
Cornelia Huck96b14532013-02-06 10:23:39 +01001244 int i;
1245
Cornelia Huck7e64e052012-12-14 17:02:18 +01001246 ccw_driver_unregister(&virtio_ccw_driver);
Cornelia Huck96b14532013-02-06 10:23:39 +01001247 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1248 destroy_airq_info(airq_areas[i]);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001249}
1250module_exit(virtio_ccw_exit);