blob: 1e1fc671f89a4a431b4a04f59fea16e302ad3a30 [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>
30#include <asm/setup.h>
31#include <asm/irq.h>
32#include <asm/cio.h>
33#include <asm/ccwdev.h>
Cornelia Huck6a773cb2013-02-28 12:33:17 +010034#include <asm/virtio-ccw.h>
Cornelia Huck96b14532013-02-06 10:23:39 +010035#include <asm/isc.h>
36#include <asm/airq.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010037
38/*
39 * virtio related functions
40 */
41
42struct vq_config_block {
43 __u16 index;
44 __u16 num;
45} __packed;
46
47#define VIRTIO_CCW_CONFIG_SIZE 0x100
48/* same as PCI config space size, should be enough for all drivers */
49
50struct virtio_ccw_device {
51 struct virtio_device vdev;
Cornelia Huck73fa21e2013-01-07 15:51:51 +010052 __u8 *status;
Cornelia Huck7e64e052012-12-14 17:02:18 +010053 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
54 struct ccw_device *cdev;
Cornelia Huck7e64e052012-12-14 17:02:18 +010055 __u32 curr_io;
56 int err;
57 wait_queue_head_t wait_q;
58 spinlock_t lock;
59 struct list_head virtqueues;
60 unsigned long indicators;
61 unsigned long indicators2;
62 struct vq_config_block *config_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010063 bool is_thinint;
Heinz Graalfs79629b22014-03-05 15:23:54 +010064 bool going_away;
Cornelia Huck96b14532013-02-06 10:23:39 +010065 void *airq_info;
Cornelia Huck7e64e052012-12-14 17:02:18 +010066};
67
68struct vq_info_block {
69 __u64 queue;
70 __u32 align;
71 __u16 index;
72 __u16 num;
73} __packed;
74
75struct virtio_feature_desc {
76 __u32 features;
77 __u8 index;
78} __packed;
79
Cornelia Huck96b14532013-02-06 10:23:39 +010080struct virtio_thinint_area {
81 unsigned long summary_indicator;
82 unsigned long indicator;
83 u64 bit_nr;
84 u8 isc;
85} __packed;
86
Cornelia Huck7e64e052012-12-14 17:02:18 +010087struct virtio_ccw_vq_info {
88 struct virtqueue *vq;
89 int num;
90 void *queue;
91 struct vq_info_block *info_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010092 int bit_nr;
Cornelia Huck7e64e052012-12-14 17:02:18 +010093 struct list_head node;
Michael S. Tsirkin07e16932013-02-28 12:33:16 +010094 long cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +010095};
96
Cornelia Huck96b14532013-02-06 10:23:39 +010097#define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
98
99#define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
100#define MAX_AIRQ_AREAS 20
101
102static int virtio_ccw_use_airq = 1;
103
104struct airq_info {
105 rwlock_t lock;
106 u8 summary_indicator;
107 struct airq_struct airq;
108 struct airq_iv *aiv;
109};
110static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
111
Cornelia Huck7e64e052012-12-14 17:02:18 +0100112#define CCW_CMD_SET_VQ 0x13
113#define CCW_CMD_VDEV_RESET 0x33
114#define CCW_CMD_SET_IND 0x43
115#define CCW_CMD_SET_CONF_IND 0x53
116#define CCW_CMD_READ_FEAT 0x12
117#define CCW_CMD_WRITE_FEAT 0x11
118#define CCW_CMD_READ_CONF 0x22
119#define CCW_CMD_WRITE_CONF 0x21
120#define CCW_CMD_WRITE_STATUS 0x31
121#define CCW_CMD_READ_VQ_CONF 0x32
Cornelia Huck96b14532013-02-06 10:23:39 +0100122#define CCW_CMD_SET_IND_ADAPTER 0x73
Cornelia Huck7e64e052012-12-14 17:02:18 +0100123
124#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
125#define VIRTIO_CCW_DOING_RESET 0x00040000
126#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
127#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
128#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
129#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
130#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
131#define VIRTIO_CCW_DOING_SET_IND 0x01000000
132#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
133#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
Cornelia Huck96b14532013-02-06 10:23:39 +0100134#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
Cornelia Huck7e64e052012-12-14 17:02:18 +0100135#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
136
137static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
138{
139 return container_of(vdev, struct virtio_ccw_device, vdev);
140}
141
Cornelia Huck96b14532013-02-06 10:23:39 +0100142static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
143{
144 unsigned long i, flags;
145
146 write_lock_irqsave(&info->lock, flags);
147 for (i = 0; i < airq_iv_end(info->aiv); i++) {
148 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
149 airq_iv_free_bit(info->aiv, i);
150 airq_iv_set_ptr(info->aiv, i, 0);
151 break;
152 }
153 }
154 write_unlock_irqrestore(&info->lock, flags);
155}
156
157static void virtio_airq_handler(struct airq_struct *airq)
158{
159 struct airq_info *info = container_of(airq, struct airq_info, airq);
160 unsigned long ai;
161
162 inc_irq_stat(IRQIO_VAI);
163 read_lock(&info->lock);
164 /* Walk through indicators field, summary indicator active. */
165 for (ai = 0;;) {
166 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
167 if (ai == -1UL)
168 break;
169 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
170 }
171 info->summary_indicator = 0;
172 smp_wmb();
173 /* Walk through indicators field, summary indicator not active. */
174 for (ai = 0;;) {
175 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
176 if (ai == -1UL)
177 break;
178 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
179 }
180 read_unlock(&info->lock);
181}
182
183static struct airq_info *new_airq_info(void)
184{
185 struct airq_info *info;
186 int rc;
187
188 info = kzalloc(sizeof(*info), GFP_KERNEL);
189 if (!info)
190 return NULL;
191 rwlock_init(&info->lock);
192 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
193 if (!info->aiv) {
194 kfree(info);
195 return NULL;
196 }
197 info->airq.handler = virtio_airq_handler;
198 info->airq.lsi_ptr = &info->summary_indicator;
199 info->airq.lsi_mask = 0xff;
200 info->airq.isc = VIRTIO_AIRQ_ISC;
201 rc = register_adapter_interrupt(&info->airq);
202 if (rc) {
203 airq_iv_release(info->aiv);
204 kfree(info);
205 return NULL;
206 }
207 return info;
208}
209
210static void destroy_airq_info(struct airq_info *info)
211{
212 if (!info)
213 return;
214
215 unregister_adapter_interrupt(&info->airq);
216 airq_iv_release(info->aiv);
217 kfree(info);
218}
219
220static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
221 u64 *first, void **airq_info)
222{
223 int i, j;
224 struct airq_info *info;
225 unsigned long indicator_addr = 0;
226 unsigned long bit, flags;
227
228 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
229 if (!airq_areas[i])
230 airq_areas[i] = new_airq_info();
231 info = airq_areas[i];
232 if (!info)
233 return 0;
234 write_lock_irqsave(&info->lock, flags);
235 bit = airq_iv_alloc(info->aiv, nvqs);
236 if (bit == -1UL) {
237 /* Not enough vacancies. */
238 write_unlock_irqrestore(&info->lock, flags);
239 continue;
240 }
241 *first = bit;
242 *airq_info = info;
243 indicator_addr = (unsigned long)info->aiv->vector;
244 for (j = 0; j < nvqs; j++) {
245 airq_iv_set_ptr(info->aiv, bit + j,
246 (unsigned long)vqs[j]);
247 }
248 write_unlock_irqrestore(&info->lock, flags);
249 }
250 return indicator_addr;
251}
252
253static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
254{
255 struct virtio_ccw_vq_info *info;
256
257 list_for_each_entry(info, &vcdev->virtqueues, node)
258 drop_airq_indicator(info->vq, vcdev->airq_info);
259}
260
Cornelia Huck7e64e052012-12-14 17:02:18 +0100261static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
262{
263 unsigned long flags;
264 __u32 ret;
265
266 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
267 if (vcdev->err)
268 ret = 0;
269 else
270 ret = vcdev->curr_io & flag;
271 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
272 return ret;
273}
274
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100275static int ccw_io_helper(struct virtio_ccw_device *vcdev,
276 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100277{
278 int ret;
279 unsigned long flags;
280 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
281
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100282 do {
283 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
284 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
Cornelia Huck99437a22013-04-04 10:25:06 +0200285 if (!ret) {
286 if (!vcdev->curr_io)
287 vcdev->err = 0;
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100288 vcdev->curr_io |= flag;
Cornelia Huck99437a22013-04-04 10:25:06 +0200289 }
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100290 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
291 cpu_relax();
292 } while (ret == -EBUSY);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100293 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
294 return ret ? ret : vcdev->err;
295}
296
Cornelia Huck96b14532013-02-06 10:23:39 +0100297static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
298 struct ccw1 *ccw)
299{
300 int ret;
301 unsigned long *indicatorp = NULL;
302 struct virtio_thinint_area *thinint_area = NULL;
303 struct airq_info *airq_info = vcdev->airq_info;
304
305 if (vcdev->is_thinint) {
306 thinint_area = kzalloc(sizeof(*thinint_area),
307 GFP_DMA | GFP_KERNEL);
308 if (!thinint_area)
309 return;
310 thinint_area->summary_indicator =
311 (unsigned long) &airq_info->summary_indicator;
312 thinint_area->isc = VIRTIO_AIRQ_ISC;
313 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
314 ccw->count = sizeof(*thinint_area);
315 ccw->cda = (__u32)(unsigned long) thinint_area;
316 } else {
317 indicatorp = kmalloc(sizeof(&vcdev->indicators),
318 GFP_DMA | GFP_KERNEL);
319 if (!indicatorp)
320 return;
321 *indicatorp = 0;
322 ccw->cmd_code = CCW_CMD_SET_IND;
323 ccw->count = sizeof(vcdev->indicators);
324 ccw->cda = (__u32)(unsigned long) indicatorp;
325 }
326 /* Deregister indicators from host. */
327 vcdev->indicators = 0;
328 ccw->flags = 0;
329 ret = ccw_io_helper(vcdev, ccw,
330 vcdev->is_thinint ?
331 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
332 VIRTIO_CCW_DOING_SET_IND);
333 if (ret && (ret != -ENODEV))
334 dev_info(&vcdev->cdev->dev,
335 "Failed to deregister indicators (%d)\n", ret);
336 else if (vcdev->is_thinint)
337 virtio_ccw_drop_indicators(vcdev);
338 kfree(indicatorp);
339 kfree(thinint_area);
340}
341
Cornelia Huck7e64e052012-12-14 17:02:18 +0100342static inline long do_kvm_notify(struct subchannel_id schid,
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100343 unsigned long queue_index,
344 long cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100345{
346 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
347 register struct subchannel_id __schid asm("2") = schid;
348 register unsigned long __index asm("3") = queue_index;
349 register long __rc asm("2");
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100350 register long __cookie asm("4") = cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100351
352 asm volatile ("diag 2,4,0x500\n"
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100353 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
354 "d"(__cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100355 : "memory", "cc");
356 return __rc;
357}
358
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030359static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100360{
361 struct virtio_ccw_vq_info *info = vq->priv;
362 struct virtio_ccw_device *vcdev;
363 struct subchannel_id schid;
364
365 vcdev = to_vc_device(info->vq->vdev);
366 ccw_device_get_schid(vcdev->cdev, &schid);
Linus Torvalds01227a82013-05-05 14:47:31 -0700367 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030368 if (info->cookie < 0)
369 return false;
370 return true;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100371}
372
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100373static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
374 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100375{
376 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100377 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
378 ccw->flags = 0;
379 ccw->count = sizeof(struct vq_config_block);
380 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
381 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100382 return vcdev->config_block->num;
383}
384
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100385static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100386{
387 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
388 struct virtio_ccw_vq_info *info = vq->priv;
389 unsigned long flags;
390 unsigned long size;
391 int ret;
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000392 unsigned int index = vq->index;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100393
394 /* Remove from our list. */
395 spin_lock_irqsave(&vcdev->lock, flags);
396 list_del(&info->node);
397 spin_unlock_irqrestore(&vcdev->lock, flags);
398
399 /* Release from host. */
400 info->info_block->queue = 0;
401 info->info_block->align = 0;
402 info->info_block->index = index;
403 info->info_block->num = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100404 ccw->cmd_code = CCW_CMD_SET_VQ;
405 ccw->flags = 0;
406 ccw->count = sizeof(*info->info_block);
407 ccw->cda = (__u32)(unsigned long)(info->info_block);
408 ret = ccw_io_helper(vcdev, ccw,
409 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100410 /*
411 * -ENODEV isn't considered an error: The device is gone anyway.
412 * This may happen on device detach.
413 */
414 if (ret && (ret != -ENODEV))
415 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
416 ret, index);
417
418 vring_del_virtqueue(vq);
419 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
420 free_pages_exact(info->queue, size);
421 kfree(info->info_block);
422 kfree(info);
423}
424
425static void virtio_ccw_del_vqs(struct virtio_device *vdev)
426{
427 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100428 struct ccw1 *ccw;
Cornelia Huck96b14532013-02-06 10:23:39 +0100429 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100430
431 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
432 if (!ccw)
433 return;
434
Cornelia Huck96b14532013-02-06 10:23:39 +0100435 virtio_ccw_drop_indicator(vcdev, ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100436
437 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100438 virtio_ccw_del_vq(vq, ccw);
439
440 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100441}
442
443static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
444 int i, vq_callback_t *callback,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100445 const char *name,
446 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100447{
448 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
449 int err;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100450 struct virtqueue *vq = NULL;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100451 struct virtio_ccw_vq_info *info;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100452 unsigned long size = 0; /* silence the compiler */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100453 unsigned long flags;
454
455 /* Allocate queue. */
456 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
457 if (!info) {
458 dev_warn(&vcdev->cdev->dev, "no info\n");
459 err = -ENOMEM;
460 goto out_err;
461 }
462 info->info_block = kzalloc(sizeof(*info->info_block),
463 GFP_DMA | GFP_KERNEL);
464 if (!info->info_block) {
465 dev_warn(&vcdev->cdev->dev, "no info block\n");
466 err = -ENOMEM;
467 goto out_err;
468 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100469 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100470 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
471 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
472 if (info->queue == NULL) {
473 dev_warn(&vcdev->cdev->dev, "no queue\n");
474 err = -ENOMEM;
475 goto out_err;
476 }
477
478 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
479 true, info->queue, virtio_ccw_kvm_notify,
480 callback, name);
481 if (!vq) {
482 /* For now, we fail if we can't get the requested size. */
483 dev_warn(&vcdev->cdev->dev, "no vq\n");
484 err = -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100485 goto out_err;
486 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100487
488 /* Register it with the host. */
489 info->info_block->queue = (__u64)info->queue;
490 info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
491 info->info_block->index = i;
492 info->info_block->num = info->num;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100493 ccw->cmd_code = CCW_CMD_SET_VQ;
494 ccw->flags = 0;
495 ccw->count = sizeof(*info->info_block);
496 ccw->cda = (__u32)(unsigned long)(info->info_block);
497 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100498 if (err) {
499 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
Cornelia Huck7e64e052012-12-14 17:02:18 +0100500 goto out_err;
501 }
502
Cornelia Huckc98d3682013-01-25 15:34:16 +0100503 info->vq = vq;
504 vq->priv = info;
505
Cornelia Huck7e64e052012-12-14 17:02:18 +0100506 /* Save it to our list. */
507 spin_lock_irqsave(&vcdev->lock, flags);
508 list_add(&info->node, &vcdev->virtqueues);
509 spin_unlock_irqrestore(&vcdev->lock, flags);
510
511 return vq;
512
513out_err:
Cornelia Huckc98d3682013-01-25 15:34:16 +0100514 if (vq)
515 vring_del_virtqueue(vq);
516 if (info) {
517 if (info->queue)
518 free_pages_exact(info->queue, size);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100519 kfree(info->info_block);
Cornelia Huckc98d3682013-01-25 15:34:16 +0100520 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100521 kfree(info);
522 return ERR_PTR(err);
523}
524
Cornelia Huck96b14532013-02-06 10:23:39 +0100525static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
526 struct virtqueue *vqs[], int nvqs,
527 struct ccw1 *ccw)
528{
529 int ret;
530 struct virtio_thinint_area *thinint_area = NULL;
531 struct airq_info *info;
532
533 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
534 if (!thinint_area) {
535 ret = -ENOMEM;
536 goto out;
537 }
538 /* Try to get an indicator. */
539 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
540 &thinint_area->bit_nr,
541 &vcdev->airq_info);
542 if (!thinint_area->indicator) {
543 ret = -ENOSPC;
544 goto out;
545 }
546 info = vcdev->airq_info;
547 thinint_area->summary_indicator =
548 (unsigned long) &info->summary_indicator;
549 thinint_area->isc = VIRTIO_AIRQ_ISC;
550 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
551 ccw->flags = CCW_FLAG_SLI;
552 ccw->count = sizeof(*thinint_area);
553 ccw->cda = (__u32)(unsigned long)thinint_area;
554 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
555 if (ret) {
556 if (ret == -EOPNOTSUPP) {
557 /*
558 * The host does not support adapter interrupts
559 * for virtio-ccw, stop trying.
560 */
561 virtio_ccw_use_airq = 0;
562 pr_info("Adapter interrupts unsupported on host\n");
563 } else
564 dev_warn(&vcdev->cdev->dev,
565 "enabling adapter interrupts = %d\n", ret);
566 virtio_ccw_drop_indicators(vcdev);
567 }
568out:
569 kfree(thinint_area);
570 return ret;
571}
572
Cornelia Huck7e64e052012-12-14 17:02:18 +0100573static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
574 struct virtqueue *vqs[],
575 vq_callback_t *callbacks[],
576 const char *names[])
577{
578 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
579 unsigned long *indicatorp = NULL;
580 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100581 struct ccw1 *ccw;
582
583 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
584 if (!ccw)
585 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100586
587 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100588 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
589 ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100590 if (IS_ERR(vqs[i])) {
591 ret = PTR_ERR(vqs[i]);
592 vqs[i] = NULL;
593 goto out;
594 }
595 }
596 ret = -ENOMEM;
597 /* We need a data area under 2G to communicate. */
598 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
599 if (!indicatorp)
600 goto out;
601 *indicatorp = (unsigned long) &vcdev->indicators;
Cornelia Huck96b14532013-02-06 10:23:39 +0100602 if (vcdev->is_thinint) {
603 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
604 if (ret)
605 /* no error, just fall back to legacy interrupts */
606 vcdev->is_thinint = 0;
607 }
608 if (!vcdev->is_thinint) {
609 /* Register queue indicators with host. */
610 vcdev->indicators = 0;
611 ccw->cmd_code = CCW_CMD_SET_IND;
612 ccw->flags = 0;
613 ccw->count = sizeof(vcdev->indicators);
614 ccw->cda = (__u32)(unsigned long) indicatorp;
615 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
616 if (ret)
617 goto out;
618 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100619 /* Register indicators2 with host for config changes */
620 *indicatorp = (unsigned long) &vcdev->indicators2;
621 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100622 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
623 ccw->flags = 0;
624 ccw->count = sizeof(vcdev->indicators2);
625 ccw->cda = (__u32)(unsigned long) indicatorp;
626 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100627 if (ret)
628 goto out;
629
630 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100631 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100632 return 0;
633out:
634 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100635 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100636 virtio_ccw_del_vqs(vdev);
637 return ret;
638}
639
640static void virtio_ccw_reset(struct virtio_device *vdev)
641{
642 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100643 struct ccw1 *ccw;
644
645 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
646 if (!ccw)
647 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100648
649 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100650 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100651
652 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100653 ccw->cmd_code = CCW_CMD_VDEV_RESET;
654 ccw->flags = 0;
655 ccw->count = 0;
656 ccw->cda = 0;
657 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
658 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100659}
660
661static u32 virtio_ccw_get_features(struct virtio_device *vdev)
662{
663 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100664 struct virtio_feature_desc *features;
665 int ret, rc;
666 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100667
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100668 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
669 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100670 return 0;
671
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100672 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
673 if (!features) {
674 rc = 0;
675 goto out_free;
676 }
677 /* Read the feature bits from the host. */
678 /* TODO: Features > 32 bits */
679 features->index = 0;
680 ccw->cmd_code = CCW_CMD_READ_FEAT;
681 ccw->flags = 0;
682 ccw->count = sizeof(*features);
683 ccw->cda = (__u32)(unsigned long)features;
684 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
685 if (ret) {
686 rc = 0;
687 goto out_free;
688 }
689
690 rc = le32_to_cpu(features->features);
691
692out_free:
693 kfree(features);
694 kfree(ccw);
695 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100696}
697
698static void virtio_ccw_finalize_features(struct virtio_device *vdev)
699{
700 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100701 struct virtio_feature_desc *features;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100702 int i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100703 struct ccw1 *ccw;
704
705 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
706 if (!ccw)
707 return;
708
709 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
710 if (!features)
711 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100712
713 /* Give virtio_ring a chance to accept features. */
714 vring_transport_features(vdev);
715
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100716 for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100717 i++) {
718 int highbits = i % 2 ? 32 : 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100719 features->index = i;
720 features->features = cpu_to_le32(vdev->features[i / 2]
721 >> highbits);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100722 /* Write the feature bits to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100723 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
724 ccw->flags = 0;
725 ccw->count = sizeof(*features);
726 ccw->cda = (__u32)(unsigned long)features;
727 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100728 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100729out_free:
730 kfree(features);
731 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100732}
733
734static void virtio_ccw_get_config(struct virtio_device *vdev,
735 unsigned int offset, void *buf, unsigned len)
736{
737 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
738 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100739 struct ccw1 *ccw;
740 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100741
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100742 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
743 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100744 return;
745
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100746 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
747 if (!config_area)
748 goto out_free;
749
750 /* Read the config area from the host. */
751 ccw->cmd_code = CCW_CMD_READ_CONF;
752 ccw->flags = 0;
753 ccw->count = offset + len;
754 ccw->cda = (__u32)(unsigned long)config_area;
755 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
756 if (ret)
757 goto out_free;
758
759 memcpy(vcdev->config, config_area, sizeof(vcdev->config));
Cornelia Huck7e64e052012-12-14 17:02:18 +0100760 memcpy(buf, &vcdev->config[offset], len);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100761
762out_free:
763 kfree(config_area);
764 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100765}
766
767static void virtio_ccw_set_config(struct virtio_device *vdev,
768 unsigned int offset, const void *buf,
769 unsigned len)
770{
771 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100772 struct ccw1 *ccw;
773 void *config_area;
774
775 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
776 if (!ccw)
777 return;
778
779 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
780 if (!config_area)
781 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100782
783 memcpy(&vcdev->config[offset], buf, len);
784 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100785 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
786 ccw->cmd_code = CCW_CMD_WRITE_CONF;
787 ccw->flags = 0;
788 ccw->count = offset + len;
789 ccw->cda = (__u32)(unsigned long)config_area;
790 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
791
792out_free:
793 kfree(config_area);
794 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100795}
796
797static u8 virtio_ccw_get_status(struct virtio_device *vdev)
798{
799 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
800
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100801 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100802}
803
804static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
805{
806 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100807 struct ccw1 *ccw;
808
809 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
810 if (!ccw)
811 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100812
813 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100814 *vcdev->status = status;
815 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
816 ccw->flags = 0;
817 ccw->count = sizeof(status);
818 ccw->cda = (__u32)(unsigned long)vcdev->status;
819 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
820 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100821}
822
823static struct virtio_config_ops virtio_ccw_config_ops = {
824 .get_features = virtio_ccw_get_features,
825 .finalize_features = virtio_ccw_finalize_features,
826 .get = virtio_ccw_get_config,
827 .set = virtio_ccw_set_config,
828 .get_status = virtio_ccw_get_status,
829 .set_status = virtio_ccw_set_status,
830 .reset = virtio_ccw_reset,
831 .find_vqs = virtio_ccw_find_vqs,
832 .del_vqs = virtio_ccw_del_vqs,
833};
834
835
836/*
837 * ccw bus driver related functions
838 */
839
840static void virtio_ccw_release_dev(struct device *_d)
841{
842 struct virtio_device *dev = container_of(_d, struct virtio_device,
843 dev);
844 struct virtio_ccw_device *vcdev = to_vc_device(dev);
845
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100846 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100847 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100848 kfree(vcdev);
849}
850
851static int irb_is_error(struct irb *irb)
852{
853 if (scsw_cstat(&irb->scsw) != 0)
854 return 1;
855 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
856 return 1;
857 if (scsw_cc(&irb->scsw) != 0)
858 return 1;
859 return 0;
860}
861
862static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
863 int index)
864{
865 struct virtio_ccw_vq_info *info;
866 unsigned long flags;
867 struct virtqueue *vq;
868
869 vq = NULL;
870 spin_lock_irqsave(&vcdev->lock, flags);
871 list_for_each_entry(info, &vcdev->virtqueues, node) {
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000872 if (info->vq->index == index) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100873 vq = info->vq;
874 break;
875 }
876 }
877 spin_unlock_irqrestore(&vcdev->lock, flags);
878 return vq;
879}
880
881static void virtio_ccw_int_handler(struct ccw_device *cdev,
882 unsigned long intparm,
883 struct irb *irb)
884{
885 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
886 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
887 int i;
888 struct virtqueue *vq;
889 struct virtio_driver *drv;
890
Heinz Graalfs2e021042014-02-27 14:34:35 +0100891 if (!vcdev)
892 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100893 /* Check if it's a notification from the host. */
894 if ((intparm == 0) &&
895 (scsw_stctl(&irb->scsw) ==
896 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
897 /* OK */
898 }
Cornelia Huck19e47352013-06-04 11:04:47 +0200899 if (irb_is_error(irb)) {
900 /* Command reject? */
901 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
902 (irb->ecw[0] & SNS0_CMD_REJECT))
903 vcdev->err = -EOPNOTSUPP;
904 else
905 /* Map everything else to -EIO. */
906 vcdev->err = -EIO;
907 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100908 if (vcdev->curr_io & activity) {
909 switch (activity) {
910 case VIRTIO_CCW_DOING_READ_FEAT:
911 case VIRTIO_CCW_DOING_WRITE_FEAT:
912 case VIRTIO_CCW_DOING_READ_CONFIG:
913 case VIRTIO_CCW_DOING_WRITE_CONFIG:
914 case VIRTIO_CCW_DOING_WRITE_STATUS:
915 case VIRTIO_CCW_DOING_SET_VQ:
916 case VIRTIO_CCW_DOING_SET_IND:
917 case VIRTIO_CCW_DOING_SET_CONF_IND:
918 case VIRTIO_CCW_DOING_RESET:
919 case VIRTIO_CCW_DOING_READ_VQ_CONF:
Cornelia Huck96b14532013-02-06 10:23:39 +0100920 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
Cornelia Huck7e64e052012-12-14 17:02:18 +0100921 vcdev->curr_io &= ~activity;
922 wake_up(&vcdev->wait_q);
923 break;
924 default:
925 /* don't know what to do... */
926 dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
927 activity);
928 WARN_ON(1);
929 break;
930 }
931 }
932 for_each_set_bit(i, &vcdev->indicators,
933 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
934 /* The bit clear must happen before the vring kick. */
935 clear_bit(i, &vcdev->indicators);
936 barrier();
937 vq = virtio_ccw_vq_by_ind(vcdev, i);
938 vring_interrupt(0, vq);
939 }
940 if (test_bit(0, &vcdev->indicators2)) {
941 drv = container_of(vcdev->vdev.dev.driver,
942 struct virtio_driver, driver);
943
944 if (drv && drv->config_changed)
945 drv->config_changed(&vcdev->vdev);
946 clear_bit(0, &vcdev->indicators2);
947 }
948}
949
950/*
951 * We usually want to autoonline all devices, but give the admin
952 * a way to exempt devices from this.
953 */
954#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
955 (8*sizeof(long)))
956static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
957
958static char *no_auto = "";
959
960module_param(no_auto, charp, 0444);
961MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
962
963static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
964{
965 struct ccw_dev_id id;
966
967 ccw_device_get_id(cdev, &id);
968 if (test_bit(id.devno, devs_no_auto[id.ssid]))
969 return 0;
970 return 1;
971}
972
973static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
974{
975 struct ccw_device *cdev = data;
976 int ret;
977
978 ret = ccw_device_set_online(cdev);
979 if (ret)
980 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
981}
982
983static int virtio_ccw_probe(struct ccw_device *cdev)
984{
985 cdev->handler = virtio_ccw_int_handler;
986
987 if (virtio_ccw_check_autoonline(cdev))
988 async_schedule(virtio_ccw_auto_online, cdev);
989 return 0;
990}
991
Heinz Graalfs2e021042014-02-27 14:34:35 +0100992static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
993{
994 unsigned long flags;
995 struct virtio_ccw_device *vcdev;
996
997 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
998 vcdev = dev_get_drvdata(&cdev->dev);
Heinz Graalfs79629b22014-03-05 15:23:54 +0100999 if (!vcdev || vcdev->going_away) {
Heinz Graalfs2e021042014-02-27 14:34:35 +01001000 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1001 return NULL;
1002 }
Heinz Graalfs79629b22014-03-05 15:23:54 +01001003 vcdev->going_away = true;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001004 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1005 return vcdev;
1006}
1007
Cornelia Huck7e64e052012-12-14 17:02:18 +01001008static void virtio_ccw_remove(struct ccw_device *cdev)
1009{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001010 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001011 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001012
Heinz Graalfs2e021042014-02-27 14:34:35 +01001013 if (vcdev && cdev->online)
Cornelia Huck7e64e052012-12-14 17:02:18 +01001014 unregister_virtio_device(&vcdev->vdev);
Heinz Graalfs79629b22014-03-05 15:23:54 +01001015 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1016 dev_set_drvdata(&cdev->dev, NULL);
1017 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001018 cdev->handler = NULL;
1019}
1020
1021static int virtio_ccw_offline(struct ccw_device *cdev)
1022{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001023 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001024 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001025
Heinz Graalfs79629b22014-03-05 15:23:54 +01001026 if (vcdev) {
Heinz Graalfs2e021042014-02-27 14:34:35 +01001027 unregister_virtio_device(&vcdev->vdev);
Heinz Graalfs79629b22014-03-05 15:23:54 +01001028 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1029 dev_set_drvdata(&cdev->dev, NULL);
1030 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1031 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001032 return 0;
1033}
1034
1035
Cornelia Huck7e64e052012-12-14 17:02:18 +01001036static int virtio_ccw_online(struct ccw_device *cdev)
1037{
1038 int ret;
1039 struct virtio_ccw_device *vcdev;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001040 unsigned long flags;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001041
1042 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1043 if (!vcdev) {
1044 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1045 ret = -ENOMEM;
1046 goto out_free;
1047 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001048 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1049 GFP_DMA | GFP_KERNEL);
1050 if (!vcdev->config_block) {
1051 ret = -ENOMEM;
1052 goto out_free;
1053 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001054 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1055 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +01001056 ret = -ENOMEM;
1057 goto out_free;
1058 }
1059
Cornelia Huck96b14532013-02-06 10:23:39 +01001060 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1061
Cornelia Huck7e64e052012-12-14 17:02:18 +01001062 vcdev->vdev.dev.parent = &cdev->dev;
1063 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1064 vcdev->vdev.config = &virtio_ccw_config_ops;
1065 vcdev->cdev = cdev;
1066 init_waitqueue_head(&vcdev->wait_q);
1067 INIT_LIST_HEAD(&vcdev->virtqueues);
1068 spin_lock_init(&vcdev->lock);
1069
Heinz Graalfs2e021042014-02-27 14:34:35 +01001070 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001071 dev_set_drvdata(&cdev->dev, vcdev);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001072 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001073 vcdev->vdev.id.vendor = cdev->id.cu_type;
1074 vcdev->vdev.id.device = cdev->id.cu_model;
1075 ret = register_virtio_device(&vcdev->vdev);
1076 if (ret) {
1077 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1078 ret);
1079 goto out_put;
1080 }
1081 return 0;
1082out_put:
Heinz Graalfs2e021042014-02-27 14:34:35 +01001083 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001084 dev_set_drvdata(&cdev->dev, NULL);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001085 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001086 put_device(&vcdev->vdev.dev);
1087 return ret;
1088out_free:
1089 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001090 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001091 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001092 }
1093 kfree(vcdev);
1094 return ret;
1095}
1096
1097static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1098{
1099 /* TODO: Check whether we need special handling here. */
1100 return 0;
1101}
1102
1103static struct ccw_device_id virtio_ids[] = {
1104 { CCW_DEVICE(0x3832, 0) },
1105 {},
1106};
1107MODULE_DEVICE_TABLE(ccw, virtio_ids);
1108
1109static struct ccw_driver virtio_ccw_driver = {
1110 .driver = {
1111 .owner = THIS_MODULE,
1112 .name = "virtio_ccw",
1113 },
1114 .ids = virtio_ids,
1115 .probe = virtio_ccw_probe,
1116 .remove = virtio_ccw_remove,
1117 .set_offline = virtio_ccw_offline,
1118 .set_online = virtio_ccw_online,
1119 .notify = virtio_ccw_cio_notify,
Linus Torvalds89f88332013-02-24 13:07:18 -08001120 .int_class = IRQIO_VIR,
Cornelia Huck7e64e052012-12-14 17:02:18 +01001121};
1122
1123static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1124 int max_digit, int max_val)
1125{
1126 int diff;
1127
1128 diff = 0;
1129 *val = 0;
1130
1131 while (diff <= max_digit) {
1132 int value = hex_to_bin(**cp);
1133
1134 if (value < 0)
1135 break;
1136 *val = *val * 16 + value;
1137 (*cp)++;
1138 diff++;
1139 }
1140
1141 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1142 return 1;
1143
1144 return 0;
1145}
1146
1147static int __init parse_busid(char *str, unsigned int *cssid,
1148 unsigned int *ssid, unsigned int *devno)
1149{
1150 char *str_work;
1151 int rc, ret;
1152
1153 rc = 1;
1154
1155 if (*str == '\0')
1156 goto out;
1157
1158 str_work = str;
1159 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1160 if (ret || (str_work[0] != '.'))
1161 goto out;
1162 str_work++;
1163 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1164 if (ret || (str_work[0] != '.'))
1165 goto out;
1166 str_work++;
1167 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1168 if (ret || (str_work[0] != '\0'))
1169 goto out;
1170
1171 rc = 0;
1172out:
1173 return rc;
1174}
1175
1176static void __init no_auto_parse(void)
1177{
1178 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1179 char *parm, *str;
1180 int rc;
1181
1182 str = no_auto;
1183 while ((parm = strsep(&str, ","))) {
1184 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1185 &from_ssid, &from);
1186 if (rc)
1187 continue;
1188 if (parm != NULL) {
1189 rc = parse_busid(parm, &to_cssid,
1190 &to_ssid, &to);
1191 if ((from_ssid > to_ssid) ||
1192 ((from_ssid == to_ssid) && (from > to)))
1193 rc = -EINVAL;
1194 } else {
1195 to_cssid = from_cssid;
1196 to_ssid = from_ssid;
1197 to = from;
1198 }
1199 if (rc)
1200 continue;
1201 while ((from_ssid < to_ssid) ||
1202 ((from_ssid == to_ssid) && (from <= to))) {
1203 set_bit(from, devs_no_auto[from_ssid]);
1204 from++;
1205 if (from > __MAX_SUBCHANNEL) {
1206 from_ssid++;
1207 from = 0;
1208 }
1209 }
1210 }
1211}
1212
1213static int __init virtio_ccw_init(void)
1214{
1215 /* parse no_auto string before we do anything further */
1216 no_auto_parse();
1217 return ccw_driver_register(&virtio_ccw_driver);
1218}
1219module_init(virtio_ccw_init);
1220
1221static void __exit virtio_ccw_exit(void)
1222{
Cornelia Huck96b14532013-02-06 10:23:39 +01001223 int i;
1224
Cornelia Huck7e64e052012-12-14 17:02:18 +01001225 ccw_driver_unregister(&virtio_ccw_driver);
Cornelia Huck96b14532013-02-06 10:23:39 +01001226 for (i = 0; i < MAX_AIRQ_AREAS; i++)
1227 destroy_airq_info(airq_areas[i]);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001228}
1229module_exit(virtio_ccw_exit);