virtio/virtio_ring: do some comment fixes
[linux-block.git] / drivers / s390 / virtio / virtio_ccw.c
CommitLineData
31fb1673 1// SPDX-License-Identifier: GPL-2.0
7e64e059
CH
2/*
3 * ccw based virtio transport
4 *
96b14536 5 * Copyright IBM Corp. 2012, 2014
7e64e059 6 *
7e64e059
CH
7 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
8 */
9
10#include <linux/kernel_stat.h>
11#include <linux/init.h>
57c8a661 12#include <linux/memblock.h>
7e64e059
CH
13#include <linux/err.h>
14#include <linux/virtio.h>
15#include <linux/virtio_config.h>
16#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/virtio_ring.h>
19#include <linux/pfn.h>
20#include <linux/async.h>
21#include <linux/wait.h>
22#include <linux/list.h>
23#include <linux/bitops.h>
acc50ec7 24#include <linux/moduleparam.h>
7e64e059
CH
25#include <linux/io.h>
26#include <linux/kvm_para.h>
e75279c4 27#include <linux/notifier.h>
1ec2772e 28#include <asm/diag.h>
7e64e059
CH
29#include <asm/setup.h>
30#include <asm/irq.h>
31#include <asm/cio.h>
32#include <asm/ccwdev.h>
6a773cb8 33#include <asm/virtio-ccw.h>
96b14536
CH
34#include <asm/isc.h>
35#include <asm/airq.h>
7e64e059
CH
36
37/*
38 * virtio related functions
39 */
40
41struct vq_config_block {
42 __u16 index;
43 __u16 num;
44} __packed;
45
46#define VIRTIO_CCW_CONFIG_SIZE 0x100
47/* same as PCI config space size, should be enough for all drivers */
48
49struct virtio_ccw_device {
50 struct virtio_device vdev;
73fa21ea 51 __u8 *status;
7e64e059
CH
52 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
53 struct ccw_device *cdev;
7e64e059
CH
54 __u32 curr_io;
55 int err;
6bb2c835 56 unsigned int revision; /* Transport revision */
7e64e059
CH
57 wait_queue_head_t wait_q;
58 spinlock_t lock;
78b1a52e 59 struct mutex io_lock; /* Serializes I/O requests */
7e64e059
CH
60 struct list_head virtqueues;
61 unsigned long indicators;
62 unsigned long indicators2;
63 struct vq_config_block *config_block;
96b14536 64 bool is_thinint;
79629b20 65 bool going_away;
e75279c4 66 bool device_lost;
431dae77 67 unsigned int config_ready;
96b14536 68 void *airq_info;
7e64e059
CH
69};
70
d4674240 71struct vq_info_block_legacy {
7e64e059
CH
72 __u64 queue;
73 __u32 align;
74 __u16 index;
75 __u16 num;
76} __packed;
77
d4674240
CH
78struct vq_info_block {
79 __u64 desc;
80 __u32 res0;
81 __u16 index;
82 __u16 num;
83 __u64 avail;
84 __u64 used;
85} __packed;
86
7e64e059 87struct virtio_feature_desc {
fb317002 88 __le32 features;
7e64e059
CH
89 __u8 index;
90} __packed;
91
96b14536
CH
92struct virtio_thinint_area {
93 unsigned long summary_indicator;
94 unsigned long indicator;
95 u64 bit_nr;
96 u8 isc;
97} __packed;
98
6bb2c835
TH
99struct virtio_rev_info {
100 __u16 revision;
101 __u16 length;
102 __u8 data[];
103};
104
105/* the highest virtio-ccw revision we support */
af9ca13b 106#define VIRTIO_CCW_REV_MAX 1
6bb2c835 107
7e64e059
CH
108struct virtio_ccw_vq_info {
109 struct virtqueue *vq;
110 int num;
111 void *queue;
d4674240
CH
112 union {
113 struct vq_info_block s;
114 struct vq_info_block_legacy l;
115 } *info_block;
96b14536 116 int bit_nr;
7e64e059 117 struct list_head node;
07e16933 118 long cookie;
7e64e059
CH
119};
120
96b14536
CH
121#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
7e64e059
CH
136#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
7d3ce5ab 146#define CCW_CMD_READ_STATUS 0x72
96b14536 147#define CCW_CMD_SET_IND_ADAPTER 0x73
6bb2c835 148#define CCW_CMD_SET_VIRTIO_REV 0x83
7e64e059
CH
149
150#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
151#define VIRTIO_CCW_DOING_RESET 0x00040000
152#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
153#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
154#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
155#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
156#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
157#define VIRTIO_CCW_DOING_SET_IND 0x01000000
158#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
159#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
96b14536 160#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
6bb2c835 161#define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
7d3ce5ab 162#define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
7e64e059
CH
163#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
164
165static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
166{
167 return container_of(vdev, struct virtio_ccw_device, vdev);
168}
169
96b14536
CH
170static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
171{
172 unsigned long i, flags;
173
174 write_lock_irqsave(&info->lock, flags);
175 for (i = 0; i < airq_iv_end(info->aiv); i++) {
176 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
177 airq_iv_free_bit(info->aiv, i);
178 airq_iv_set_ptr(info->aiv, i, 0);
179 break;
180 }
181 }
182 write_unlock_irqrestore(&info->lock, flags);
183}
184
185static void virtio_airq_handler(struct airq_struct *airq)
186{
187 struct airq_info *info = container_of(airq, struct airq_info, airq);
188 unsigned long ai;
189
190 inc_irq_stat(IRQIO_VAI);
191 read_lock(&info->lock);
192 /* Walk through indicators field, summary indicator active. */
193 for (ai = 0;;) {
194 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
195 if (ai == -1UL)
196 break;
197 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
198 }
199 info->summary_indicator = 0;
200 smp_wmb();
201 /* Walk through indicators field, summary indicator not active. */
202 for (ai = 0;;) {
203 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
204 if (ai == -1UL)
205 break;
206 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
207 }
208 read_unlock(&info->lock);
209}
210
211static struct airq_info *new_airq_info(void)
212{
213 struct airq_info *info;
214 int rc;
215
216 info = kzalloc(sizeof(*info), GFP_KERNEL);
217 if (!info)
218 return NULL;
219 rwlock_init(&info->lock);
220 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
221 if (!info->aiv) {
222 kfree(info);
223 return NULL;
224 }
225 info->airq.handler = virtio_airq_handler;
226 info->airq.lsi_ptr = &info->summary_indicator;
227 info->airq.lsi_mask = 0xff;
228 info->airq.isc = VIRTIO_AIRQ_ISC;
229 rc = register_adapter_interrupt(&info->airq);
230 if (rc) {
231 airq_iv_release(info->aiv);
232 kfree(info);
233 return NULL;
234 }
235 return info;
236}
237
96b14536
CH
238static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
239 u64 *first, void **airq_info)
240{
241 int i, j;
242 struct airq_info *info;
243 unsigned long indicator_addr = 0;
244 unsigned long bit, flags;
245
246 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
247 if (!airq_areas[i])
248 airq_areas[i] = new_airq_info();
249 info = airq_areas[i];
250 if (!info)
251 return 0;
252 write_lock_irqsave(&info->lock, flags);
253 bit = airq_iv_alloc(info->aiv, nvqs);
254 if (bit == -1UL) {
255 /* Not enough vacancies. */
256 write_unlock_irqrestore(&info->lock, flags);
257 continue;
258 }
259 *first = bit;
260 *airq_info = info;
261 indicator_addr = (unsigned long)info->aiv->vector;
262 for (j = 0; j < nvqs; j++) {
263 airq_iv_set_ptr(info->aiv, bit + j,
264 (unsigned long)vqs[j]);
265 }
266 write_unlock_irqrestore(&info->lock, flags);
267 }
268 return indicator_addr;
269}
270
271static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
272{
273 struct virtio_ccw_vq_info *info;
274
3438b2c0
HP
275 if (!vcdev->airq_info)
276 return;
96b14536
CH
277 list_for_each_entry(info, &vcdev->virtqueues, node)
278 drop_airq_indicator(info->vq, vcdev->airq_info);
279}
280
7e64e059
CH
281static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
282{
283 unsigned long flags;
284 __u32 ret;
285
286 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
287 if (vcdev->err)
288 ret = 0;
289 else
290 ret = vcdev->curr_io & flag;
291 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
292 return ret;
293}
294
73fa21ea
CH
295static int ccw_io_helper(struct virtio_ccw_device *vcdev,
296 struct ccw1 *ccw, __u32 intparm)
7e64e059
CH
297{
298 int ret;
299 unsigned long flags;
300 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
301
78b1a52e 302 mutex_lock(&vcdev->io_lock);
b26ba22b
CB
303 do {
304 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
305 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
99437a27
CH
306 if (!ret) {
307 if (!vcdev->curr_io)
308 vcdev->err = 0;
b26ba22b 309 vcdev->curr_io |= flag;
99437a27 310 }
b26ba22b
CB
311 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
312 cpu_relax();
313 } while (ret == -EBUSY);
7e64e059 314 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
78b1a52e
HP
315 ret = ret ? ret : vcdev->err;
316 mutex_unlock(&vcdev->io_lock);
317 return ret;
7e64e059
CH
318}
319
96b14536
CH
320static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
321 struct ccw1 *ccw)
322{
323 int ret;
324 unsigned long *indicatorp = NULL;
325 struct virtio_thinint_area *thinint_area = NULL;
326 struct airq_info *airq_info = vcdev->airq_info;
327
328 if (vcdev->is_thinint) {
329 thinint_area = kzalloc(sizeof(*thinint_area),
330 GFP_DMA | GFP_KERNEL);
331 if (!thinint_area)
332 return;
333 thinint_area->summary_indicator =
334 (unsigned long) &airq_info->summary_indicator;
335 thinint_area->isc = VIRTIO_AIRQ_ISC;
336 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
337 ccw->count = sizeof(*thinint_area);
338 ccw->cda = (__u32)(unsigned long) thinint_area;
339 } else {
c235082b 340 /* payload is the address of the indicators */
96b14536
CH
341 indicatorp = kmalloc(sizeof(&vcdev->indicators),
342 GFP_DMA | GFP_KERNEL);
343 if (!indicatorp)
344 return;
345 *indicatorp = 0;
346 ccw->cmd_code = CCW_CMD_SET_IND;
c235082b 347 ccw->count = sizeof(&vcdev->indicators);
96b14536
CH
348 ccw->cda = (__u32)(unsigned long) indicatorp;
349 }
350 /* Deregister indicators from host. */
351 vcdev->indicators = 0;
352 ccw->flags = 0;
353 ret = ccw_io_helper(vcdev, ccw,
354 vcdev->is_thinint ?
355 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
356 VIRTIO_CCW_DOING_SET_IND);
357 if (ret && (ret != -ENODEV))
358 dev_info(&vcdev->cdev->dev,
359 "Failed to deregister indicators (%d)\n", ret);
360 else if (vcdev->is_thinint)
361 virtio_ccw_drop_indicators(vcdev);
362 kfree(indicatorp);
363 kfree(thinint_area);
364}
365
1ec2772e
MS
366static inline long __do_kvm_notify(struct subchannel_id schid,
367 unsigned long queue_index,
368 long cookie)
7e64e059
CH
369{
370 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
371 register struct subchannel_id __schid asm("2") = schid;
372 register unsigned long __index asm("3") = queue_index;
373 register long __rc asm("2");
07e16933 374 register long __cookie asm("4") = cookie;
7e64e059
CH
375
376 asm volatile ("diag 2,4,0x500\n"
07e16933
MT
377 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
378 "d"(__cookie)
7e64e059
CH
379 : "memory", "cc");
380 return __rc;
381}
382
1ec2772e
MS
383static inline long do_kvm_notify(struct subchannel_id schid,
384 unsigned long queue_index,
385 long cookie)
386{
387 diag_stat_inc(DIAG_STAT_X500);
388 return __do_kvm_notify(schid, queue_index, cookie);
389}
390
46f9c2b9 391static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
7e64e059
CH
392{
393 struct virtio_ccw_vq_info *info = vq->priv;
394 struct virtio_ccw_device *vcdev;
395 struct subchannel_id schid;
396
397 vcdev = to_vc_device(info->vq->vdev);
398 ccw_device_get_schid(vcdev->cdev, &schid);
01227a88 399 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
46f9c2b9
HG
400 if (info->cookie < 0)
401 return false;
402 return true;
7e64e059
CH
403}
404
73fa21ea
CH
405static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
406 struct ccw1 *ccw, int index)
7e64e059 407{
ad2aa042
PM
408 int ret;
409
7e64e059 410 vcdev->config_block->index = index;
73fa21ea
CH
411 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
412 ccw->flags = 0;
413 ccw->count = sizeof(struct vq_config_block);
414 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
ad2aa042
PM
415 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
416 if (ret)
417 return ret;
3438b2c0 418 return vcdev->config_block->num ?: -ENOENT;
7e64e059
CH
419}
420
73fa21ea 421static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
7e64e059
CH
422{
423 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
424 struct virtio_ccw_vq_info *info = vq->priv;
425 unsigned long flags;
426 unsigned long size;
427 int ret;
9d0ca6ed 428 unsigned int index = vq->index;
7e64e059
CH
429
430 /* Remove from our list. */
431 spin_lock_irqsave(&vcdev->lock, flags);
432 list_del(&info->node);
433 spin_unlock_irqrestore(&vcdev->lock, flags);
434
435 /* Release from host. */
d4674240
CH
436 if (vcdev->revision == 0) {
437 info->info_block->l.queue = 0;
438 info->info_block->l.align = 0;
439 info->info_block->l.index = index;
440 info->info_block->l.num = 0;
441 ccw->count = sizeof(info->info_block->l);
442 } else {
443 info->info_block->s.desc = 0;
444 info->info_block->s.index = index;
445 info->info_block->s.num = 0;
446 info->info_block->s.avail = 0;
447 info->info_block->s.used = 0;
448 ccw->count = sizeof(info->info_block->s);
449 }
73fa21ea
CH
450 ccw->cmd_code = CCW_CMD_SET_VQ;
451 ccw->flags = 0;
73fa21ea
CH
452 ccw->cda = (__u32)(unsigned long)(info->info_block);
453 ret = ccw_io_helper(vcdev, ccw,
454 VIRTIO_CCW_DOING_SET_VQ | index);
7e64e059
CH
455 /*
456 * -ENODEV isn't considered an error: The device is gone anyway.
457 * This may happen on device detach.
458 */
459 if (ret && (ret != -ENODEV))
99240622 460 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
7e64e059
CH
461 ret, index);
462
463 vring_del_virtqueue(vq);
464 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
465 free_pages_exact(info->queue, size);
466 kfree(info->info_block);
467 kfree(info);
468}
469
470static void virtio_ccw_del_vqs(struct virtio_device *vdev)
471{
472 struct virtqueue *vq, *n;
73fa21ea 473 struct ccw1 *ccw;
96b14536 474 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
475
476 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
477 if (!ccw)
478 return;
479
96b14536 480 virtio_ccw_drop_indicator(vcdev, ccw);
7e64e059
CH
481
482 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
73fa21ea
CH
483 virtio_ccw_del_vq(vq, ccw);
484
485 kfree(ccw);
7e64e059
CH
486}
487
488static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
489 int i, vq_callback_t *callback,
f94682dd 490 const char *name, bool ctx,
73fa21ea 491 struct ccw1 *ccw)
7e64e059
CH
492{
493 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
494 int err;
c98d3683 495 struct virtqueue *vq = NULL;
7e64e059 496 struct virtio_ccw_vq_info *info;
c98d3683 497 unsigned long size = 0; /* silence the compiler */
7e64e059
CH
498 unsigned long flags;
499
500 /* Allocate queue. */
501 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
502 if (!info) {
503 dev_warn(&vcdev->cdev->dev, "no info\n");
504 err = -ENOMEM;
505 goto out_err;
506 }
507 info->info_block = kzalloc(sizeof(*info->info_block),
508 GFP_DMA | GFP_KERNEL);
509 if (!info->info_block) {
510 dev_warn(&vcdev->cdev->dev, "no info block\n");
511 err = -ENOMEM;
512 goto out_err;
513 }
73fa21ea 514 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
ad2aa042
PM
515 if (info->num < 0) {
516 err = info->num;
517 goto out_err;
518 }
7e64e059
CH
519 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
520 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
521 if (info->queue == NULL) {
522 dev_warn(&vcdev->cdev->dev, "no queue\n");
523 err = -ENOMEM;
524 goto out_err;
525 }
526
527 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
f94682dd 528 true, ctx, info->queue, virtio_ccw_kvm_notify,
7e64e059
CH
529 callback, name);
530 if (!vq) {
531 /* For now, we fail if we can't get the requested size. */
532 dev_warn(&vcdev->cdev->dev, "no vq\n");
533 err = -ENOMEM;
7e64e059
CH
534 goto out_err;
535 }
7e64e059
CH
536
537 /* Register it with the host. */
d4674240
CH
538 if (vcdev->revision == 0) {
539 info->info_block->l.queue = (__u64)info->queue;
540 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
541 info->info_block->l.index = i;
542 info->info_block->l.num = info->num;
543 ccw->count = sizeof(info->info_block->l);
544 } else {
545 info->info_block->s.desc = (__u64)info->queue;
546 info->info_block->s.index = i;
547 info->info_block->s.num = info->num;
548 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
549 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
550 ccw->count = sizeof(info->info_block->s);
551 }
73fa21ea
CH
552 ccw->cmd_code = CCW_CMD_SET_VQ;
553 ccw->flags = 0;
73fa21ea
CH
554 ccw->cda = (__u32)(unsigned long)(info->info_block);
555 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
7e64e059
CH
556 if (err) {
557 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
7e64e059
CH
558 goto out_err;
559 }
560
c98d3683
CH
561 info->vq = vq;
562 vq->priv = info;
563
7e64e059
CH
564 /* Save it to our list. */
565 spin_lock_irqsave(&vcdev->lock, flags);
566 list_add(&info->node, &vcdev->virtqueues);
567 spin_unlock_irqrestore(&vcdev->lock, flags);
568
569 return vq;
570
571out_err:
c98d3683
CH
572 if (vq)
573 vring_del_virtqueue(vq);
574 if (info) {
575 if (info->queue)
576 free_pages_exact(info->queue, size);
7e64e059 577 kfree(info->info_block);
c98d3683 578 }
7e64e059
CH
579 kfree(info);
580 return ERR_PTR(err);
581}
582
96b14536
CH
583static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
584 struct virtqueue *vqs[], int nvqs,
585 struct ccw1 *ccw)
586{
587 int ret;
588 struct virtio_thinint_area *thinint_area = NULL;
589 struct airq_info *info;
590
591 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
592 if (!thinint_area) {
593 ret = -ENOMEM;
594 goto out;
595 }
596 /* Try to get an indicator. */
597 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
598 &thinint_area->bit_nr,
599 &vcdev->airq_info);
600 if (!thinint_area->indicator) {
601 ret = -ENOSPC;
602 goto out;
603 }
604 info = vcdev->airq_info;
605 thinint_area->summary_indicator =
606 (unsigned long) &info->summary_indicator;
607 thinint_area->isc = VIRTIO_AIRQ_ISC;
608 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
609 ccw->flags = CCW_FLAG_SLI;
610 ccw->count = sizeof(*thinint_area);
611 ccw->cda = (__u32)(unsigned long)thinint_area;
612 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
613 if (ret) {
614 if (ret == -EOPNOTSUPP) {
615 /*
616 * The host does not support adapter interrupts
617 * for virtio-ccw, stop trying.
618 */
619 virtio_ccw_use_airq = 0;
620 pr_info("Adapter interrupts unsupported on host\n");
621 } else
622 dev_warn(&vcdev->cdev->dev,
623 "enabling adapter interrupts = %d\n", ret);
624 virtio_ccw_drop_indicators(vcdev);
625 }
626out:
627 kfree(thinint_area);
628 return ret;
629}
630
7e64e059
CH
631static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
632 struct virtqueue *vqs[],
633 vq_callback_t *callbacks[],
fb5e31d9 634 const char * const names[],
f94682dd 635 const bool *ctx,
fb5e31d9 636 struct irq_affinity *desc)
7e64e059
CH
637{
638 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
639 unsigned long *indicatorp = NULL;
a229989d 640 int ret, i, queue_idx = 0;
73fa21ea
CH
641 struct ccw1 *ccw;
642
643 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
644 if (!ccw)
645 return -ENOMEM;
7e64e059
CH
646
647 for (i = 0; i < nvqs; ++i) {
a229989d
WW
648 if (!names[i]) {
649 vqs[i] = NULL;
650 continue;
651 }
652
653 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, callbacks[i],
654 names[i], ctx ? ctx[i] : false,
655 ccw);
7e64e059
CH
656 if (IS_ERR(vqs[i])) {
657 ret = PTR_ERR(vqs[i]);
658 vqs[i] = NULL;
659 goto out;
660 }
661 }
662 ret = -ENOMEM;
c235082b
CH
663 /*
664 * We need a data area under 2G to communicate. Our payload is
665 * the address of the indicators.
666 */
7e64e059
CH
667 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
668 if (!indicatorp)
669 goto out;
670 *indicatorp = (unsigned long) &vcdev->indicators;
96b14536
CH
671 if (vcdev->is_thinint) {
672 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
673 if (ret)
674 /* no error, just fall back to legacy interrupts */
970ba6ac 675 vcdev->is_thinint = false;
96b14536
CH
676 }
677 if (!vcdev->is_thinint) {
678 /* Register queue indicators with host. */
679 vcdev->indicators = 0;
680 ccw->cmd_code = CCW_CMD_SET_IND;
681 ccw->flags = 0;
c235082b 682 ccw->count = sizeof(&vcdev->indicators);
96b14536
CH
683 ccw->cda = (__u32)(unsigned long) indicatorp;
684 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
685 if (ret)
686 goto out;
687 }
7e64e059
CH
688 /* Register indicators2 with host for config changes */
689 *indicatorp = (unsigned long) &vcdev->indicators2;
690 vcdev->indicators2 = 0;
73fa21ea
CH
691 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
692 ccw->flags = 0;
c235082b 693 ccw->count = sizeof(&vcdev->indicators2);
73fa21ea
CH
694 ccw->cda = (__u32)(unsigned long) indicatorp;
695 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
7e64e059
CH
696 if (ret)
697 goto out;
698
699 kfree(indicatorp);
73fa21ea 700 kfree(ccw);
7e64e059
CH
701 return 0;
702out:
703 kfree(indicatorp);
73fa21ea 704 kfree(ccw);
7e64e059
CH
705 virtio_ccw_del_vqs(vdev);
706 return ret;
707}
708
709static void virtio_ccw_reset(struct virtio_device *vdev)
710{
711 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
712 struct ccw1 *ccw;
713
714 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
715 if (!ccw)
716 return;
7e64e059
CH
717
718 /* Zero status bits. */
73fa21ea 719 *vcdev->status = 0;
7e64e059
CH
720
721 /* Send a reset ccw on device. */
73fa21ea
CH
722 ccw->cmd_code = CCW_CMD_VDEV_RESET;
723 ccw->flags = 0;
724 ccw->count = 0;
725 ccw->cda = 0;
726 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
727 kfree(ccw);
7e64e059
CH
728}
729
d0254773 730static u64 virtio_ccw_get_features(struct virtio_device *vdev)
7e64e059
CH
731{
732 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea 733 struct virtio_feature_desc *features;
732c56e9
MT
734 int ret;
735 u64 rc;
73fa21ea 736 struct ccw1 *ccw;
7e64e059 737
73fa21ea
CH
738 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
739 if (!ccw)
740 return 0;
741
742 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
743 if (!features) {
744 rc = 0;
745 goto out_free;
746 }
7e64e059 747 /* Read the feature bits from the host. */
73fa21ea
CH
748 features->index = 0;
749 ccw->cmd_code = CCW_CMD_READ_FEAT;
750 ccw->flags = 0;
751 ccw->count = sizeof(*features);
752 ccw->cda = (__u32)(unsigned long)features;
753 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
754 if (ret) {
755 rc = 0;
756 goto out_free;
757 }
758
759 rc = le32_to_cpu(features->features);
7e64e059 760
ce15408f
MT
761 if (vcdev->revision == 0)
762 goto out_free;
763
732c56e9
MT
764 /* Read second half of the feature bits from the host. */
765 features->index = 1;
766 ccw->cmd_code = CCW_CMD_READ_FEAT;
767 ccw->flags = 0;
768 ccw->count = sizeof(*features);
769 ccw->cda = (__u32)(unsigned long)features;
770 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
771 if (ret == 0)
772 rc |= (u64)le32_to_cpu(features->features) << 32;
773
73fa21ea
CH
774out_free:
775 kfree(features);
776 kfree(ccw);
777 return rc;
7e64e059
CH
778}
779
3a814fdf
TB
780static void ccw_transport_features(struct virtio_device *vdev)
781{
782 /*
783 * Packed ring isn't enabled on virtio_ccw for now,
784 * because virtio_ccw uses some legacy accessors,
785 * e.g. virtqueue_get_avail() and virtqueue_get_used()
786 * which aren't available in packed ring currently.
787 */
788 __virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED);
789}
790
5c609a5e 791static int virtio_ccw_finalize_features(struct virtio_device *vdev)
7e64e059
CH
792{
793 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea 794 struct virtio_feature_desc *features;
73fa21ea 795 struct ccw1 *ccw;
f01a2a81 796 int ret;
73fa21ea 797
f13d8bc2 798 if (vcdev->revision >= 1 &&
4d23676f
MT
799 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
800 dev_err(&vdev->dev, "virtio: device uses revision 1 "
801 "but does not have VIRTIO_F_VERSION_1\n");
802 return -EINVAL;
803 }
804
73fa21ea
CH
805 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
806 if (!ccw)
f01a2a81 807 return -ENOMEM;
73fa21ea
CH
808
809 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
f01a2a81
CH
810 if (!features) {
811 ret = -ENOMEM;
73fa21ea 812 goto out_free;
f01a2a81 813 }
7e64e059
CH
814 /* Give virtio_ring a chance to accept features. */
815 vring_transport_features(vdev);
816
3a814fdf
TB
817 /* Give virtio_ccw a chance to accept features. */
818 ccw_transport_features(vdev);
819
e16e12be 820 features->index = 0;
732c56e9
MT
821 features->features = cpu_to_le32((u32)vdev->features);
822 /* Write the first half of the feature bits to the host. */
823 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
824 ccw->flags = 0;
825 ccw->count = sizeof(*features);
826 ccw->cda = (__u32)(unsigned long)features;
f01a2a81
CH
827 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
828 if (ret)
829 goto out_free;
732c56e9 830
ce15408f
MT
831 if (vcdev->revision == 0)
832 goto out_free;
833
732c56e9
MT
834 features->index = 1;
835 features->features = cpu_to_le32(vdev->features >> 32);
836 /* Write the second half of the feature bits to the host. */
e16e12be
MT
837 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
838 ccw->flags = 0;
839 ccw->count = sizeof(*features);
840 ccw->cda = (__u32)(unsigned long)features;
f01a2a81 841 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
e16e12be 842
73fa21ea
CH
843out_free:
844 kfree(features);
845 kfree(ccw);
5c609a5e 846
f01a2a81 847 return ret;
7e64e059
CH
848}
849
850static void virtio_ccw_get_config(struct virtio_device *vdev,
851 unsigned int offset, void *buf, unsigned len)
852{
853 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
854 int ret;
73fa21ea
CH
855 struct ccw1 *ccw;
856 void *config_area;
2448a299 857 unsigned long flags;
73fa21ea
CH
858
859 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
860 if (!ccw)
861 return;
862
863 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
864 if (!config_area)
865 goto out_free;
7e64e059
CH
866
867 /* Read the config area from the host. */
73fa21ea
CH
868 ccw->cmd_code = CCW_CMD_READ_CONF;
869 ccw->flags = 0;
870 ccw->count = offset + len;
871 ccw->cda = (__u32)(unsigned long)config_area;
872 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
7e64e059 873 if (ret)
73fa21ea 874 goto out_free;
7e64e059 875
2448a299 876 spin_lock_irqsave(&vcdev->lock, flags);
431dae77 877 memcpy(vcdev->config, config_area, offset + len);
431dae77
CH
878 if (vcdev->config_ready < offset + len)
879 vcdev->config_ready = offset + len;
2448a299
HP
880 spin_unlock_irqrestore(&vcdev->lock, flags);
881 if (buf)
882 memcpy(buf, config_area + offset, len);
73fa21ea
CH
883
884out_free:
885 kfree(config_area);
886 kfree(ccw);
7e64e059
CH
887}
888
889static void virtio_ccw_set_config(struct virtio_device *vdev,
890 unsigned int offset, const void *buf,
891 unsigned len)
892{
893 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
73fa21ea
CH
894 struct ccw1 *ccw;
895 void *config_area;
2448a299 896 unsigned long flags;
73fa21ea
CH
897
898 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
899 if (!ccw)
900 return;
901
902 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
903 if (!config_area)
904 goto out_free;
7e64e059 905
431dae77
CH
906 /* Make sure we don't overwrite fields. */
907 if (vcdev->config_ready < offset)
908 virtio_ccw_get_config(vdev, 0, NULL, offset);
2448a299 909 spin_lock_irqsave(&vcdev->lock, flags);
7e64e059
CH
910 memcpy(&vcdev->config[offset], buf, len);
911 /* Write the config area to the host. */
73fa21ea 912 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
2448a299 913 spin_unlock_irqrestore(&vcdev->lock, flags);
73fa21ea
CH
914 ccw->cmd_code = CCW_CMD_WRITE_CONF;
915 ccw->flags = 0;
916 ccw->count = offset + len;
917 ccw->cda = (__u32)(unsigned long)config_area;
918 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
919
920out_free:
921 kfree(config_area);
922 kfree(ccw);
7e64e059
CH
923}
924
925static u8 virtio_ccw_get_status(struct virtio_device *vdev)
926{
927 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
7d3ce5ab
PM
928 u8 old_status = *vcdev->status;
929 struct ccw1 *ccw;
930
931 if (vcdev->revision < 1)
932 return *vcdev->status;
933
934 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
935 if (!ccw)
936 return old_status;
937
938 ccw->cmd_code = CCW_CMD_READ_STATUS;
939 ccw->flags = 0;
940 ccw->count = sizeof(*vcdev->status);
941 ccw->cda = (__u32)(unsigned long)vcdev->status;
942 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
943/*
944 * If the channel program failed (should only happen if the device
945 * was hotunplugged, and then we clean up via the machine check
946 * handler anyway), vcdev->status was not overwritten and we just
947 * return the old status, which is fine.
948*/
949 kfree(ccw);
7e64e059 950
73fa21ea 951 return *vcdev->status;
7e64e059
CH
952}
953
954static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
955{
956 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
48555136 957 u8 old_status = *vcdev->status;
73fa21ea 958 struct ccw1 *ccw;
48555136 959 int ret;
73fa21ea
CH
960
961 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
962 if (!ccw)
963 return;
7e64e059
CH
964
965 /* Write the status to the host. */
73fa21ea
CH
966 *vcdev->status = status;
967 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
968 ccw->flags = 0;
969 ccw->count = sizeof(status);
970 ccw->cda = (__u32)(unsigned long)vcdev->status;
48555136
MT
971 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
972 /* Write failed? We assume status is unchanged. */
973 if (ret)
974 *vcdev->status = old_status;
73fa21ea 975 kfree(ccw);
7e64e059
CH
976}
977
971bedca
CH
978static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
979{
980 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
981
982 return dev_name(&vcdev->cdev->dev);
983}
984
0db1dba5 985static const struct virtio_config_ops virtio_ccw_config_ops = {
7e64e059
CH
986 .get_features = virtio_ccw_get_features,
987 .finalize_features = virtio_ccw_finalize_features,
988 .get = virtio_ccw_get_config,
989 .set = virtio_ccw_set_config,
990 .get_status = virtio_ccw_get_status,
991 .set_status = virtio_ccw_set_status,
992 .reset = virtio_ccw_reset,
993 .find_vqs = virtio_ccw_find_vqs,
994 .del_vqs = virtio_ccw_del_vqs,
971bedca 995 .bus_name = virtio_ccw_bus_name,
7e64e059
CH
996};
997
998
999/*
1000 * ccw bus driver related functions
1001 */
1002
1003static void virtio_ccw_release_dev(struct device *_d)
1004{
15e90f52 1005 struct virtio_device *dev = dev_to_virtio(_d);
7e64e059
CH
1006 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1007
73fa21ea 1008 kfree(vcdev->status);
7e64e059 1009 kfree(vcdev->config_block);
7e64e059
CH
1010 kfree(vcdev);
1011}
1012
1013static int irb_is_error(struct irb *irb)
1014{
1015 if (scsw_cstat(&irb->scsw) != 0)
1016 return 1;
1017 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1018 return 1;
1019 if (scsw_cc(&irb->scsw) != 0)
1020 return 1;
1021 return 0;
1022}
1023
1024static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1025 int index)
1026{
1027 struct virtio_ccw_vq_info *info;
1028 unsigned long flags;
1029 struct virtqueue *vq;
1030
1031 vq = NULL;
1032 spin_lock_irqsave(&vcdev->lock, flags);
1033 list_for_each_entry(info, &vcdev->virtqueues, node) {
9d0ca6ed 1034 if (info->vq->index == index) {
7e64e059
CH
1035 vq = info->vq;
1036 break;
1037 }
1038 }
1039 spin_unlock_irqrestore(&vcdev->lock, flags);
1040 return vq;
1041}
1042
74a599f0
CH
1043static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1044 __u32 activity)
1045{
1046 if (vcdev->curr_io & activity) {
1047 switch (activity) {
1048 case VIRTIO_CCW_DOING_READ_FEAT:
1049 case VIRTIO_CCW_DOING_WRITE_FEAT:
1050 case VIRTIO_CCW_DOING_READ_CONFIG:
1051 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1052 case VIRTIO_CCW_DOING_WRITE_STATUS:
7d3ce5ab 1053 case VIRTIO_CCW_DOING_READ_STATUS:
74a599f0
CH
1054 case VIRTIO_CCW_DOING_SET_VQ:
1055 case VIRTIO_CCW_DOING_SET_IND:
1056 case VIRTIO_CCW_DOING_SET_CONF_IND:
1057 case VIRTIO_CCW_DOING_RESET:
1058 case VIRTIO_CCW_DOING_READ_VQ_CONF:
1059 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1060 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1061 vcdev->curr_io &= ~activity;
1062 wake_up(&vcdev->wait_q);
1063 break;
1064 default:
1065 /* don't know what to do... */
1066 dev_warn(&vcdev->cdev->dev,
1067 "Suspicious activity '%08x'\n", activity);
1068 WARN_ON(1);
1069 break;
1070 }
1071 }
1072}
1073
7e64e059
CH
1074static void virtio_ccw_int_handler(struct ccw_device *cdev,
1075 unsigned long intparm,
1076 struct irb *irb)
1077{
1078 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1079 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1080 int i;
1081 struct virtqueue *vq;
7e64e059 1082
2e021043
HG
1083 if (!vcdev)
1084 return;
74a599f0
CH
1085 if (IS_ERR(irb)) {
1086 vcdev->err = PTR_ERR(irb);
1087 virtio_ccw_check_activity(vcdev, activity);
1088 /* Don't poke around indicators, something's wrong. */
1089 return;
1090 }
7e64e059
CH
1091 /* Check if it's a notification from the host. */
1092 if ((intparm == 0) &&
1093 (scsw_stctl(&irb->scsw) ==
1094 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1095 /* OK */
1096 }
19e4735b
CH
1097 if (irb_is_error(irb)) {
1098 /* Command reject? */
1099 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1100 (irb->ecw[0] & SNS0_CMD_REJECT))
1101 vcdev->err = -EOPNOTSUPP;
1102 else
1103 /* Map everything else to -EIO. */
1104 vcdev->err = -EIO;
1105 }
74a599f0 1106 virtio_ccw_check_activity(vcdev, activity);
7e64e059
CH
1107 for_each_set_bit(i, &vcdev->indicators,
1108 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1109 /* The bit clear must happen before the vring kick. */
1110 clear_bit(i, &vcdev->indicators);
1111 barrier();
1112 vq = virtio_ccw_vq_by_ind(vcdev, i);
1113 vring_interrupt(0, vq);
1114 }
1115 if (test_bit(0, &vcdev->indicators2)) {
016c98c6 1116 virtio_config_changed(&vcdev->vdev);
7e64e059
CH
1117 clear_bit(0, &vcdev->indicators2);
1118 }
1119}
1120
1121/*
1122 * We usually want to autoonline all devices, but give the admin
1123 * a way to exempt devices from this.
1124 */
1125#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1126 (8*sizeof(long)))
1127static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1128
1129static char *no_auto = "";
1130
1131module_param(no_auto, charp, 0444);
1132MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1133
1134static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1135{
1136 struct ccw_dev_id id;
1137
1138 ccw_device_get_id(cdev, &id);
1139 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1140 return 0;
1141 return 1;
1142}
1143
1144static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1145{
1146 struct ccw_device *cdev = data;
1147 int ret;
1148
1149 ret = ccw_device_set_online(cdev);
1150 if (ret)
1151 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1152}
1153
1154static int virtio_ccw_probe(struct ccw_device *cdev)
1155{
1156 cdev->handler = virtio_ccw_int_handler;
1157
1158 if (virtio_ccw_check_autoonline(cdev))
1159 async_schedule(virtio_ccw_auto_online, cdev);
1160 return 0;
1161}
1162
2e021043
HG
1163static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1164{
1165 unsigned long flags;
1166 struct virtio_ccw_device *vcdev;
1167
1168 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1169 vcdev = dev_get_drvdata(&cdev->dev);
79629b20 1170 if (!vcdev || vcdev->going_away) {
2e021043
HG
1171 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1172 return NULL;
1173 }
79629b20 1174 vcdev->going_away = true;
2e021043
HG
1175 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1176 return vcdev;
1177}
1178
7e64e059
CH
1179static void virtio_ccw_remove(struct ccw_device *cdev)
1180{
79629b20 1181 unsigned long flags;
2e021043 1182 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
7e64e059 1183
e75279c4
HG
1184 if (vcdev && cdev->online) {
1185 if (vcdev->device_lost)
1186 virtio_break_device(&vcdev->vdev);
7e64e059 1187 unregister_virtio_device(&vcdev->vdev);
e75279c4
HG
1188 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1189 dev_set_drvdata(&cdev->dev, NULL);
1190 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1191 }
7e64e059
CH
1192 cdev->handler = NULL;
1193}
1194
1195static int virtio_ccw_offline(struct ccw_device *cdev)
1196{
79629b20 1197 unsigned long flags;
2e021043 1198 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
7e64e059 1199
e75279c4
HG
1200 if (!vcdev)
1201 return 0;
1202 if (vcdev->device_lost)
1203 virtio_break_device(&vcdev->vdev);
1204 unregister_virtio_device(&vcdev->vdev);
1205 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1206 dev_set_drvdata(&cdev->dev, NULL);
1207 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
7e64e059
CH
1208 return 0;
1209}
1210
6bb2c835
TH
1211static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1212{
1213 struct virtio_rev_info *rev;
1214 struct ccw1 *ccw;
1215 int ret;
1216
1217 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1218 if (!ccw)
1219 return -ENOMEM;
1220 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1221 if (!rev) {
1222 kfree(ccw);
1223 return -ENOMEM;
1224 }
1225
1226 /* Set transport revision */
1227 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1228 ccw->flags = 0;
1229 ccw->count = sizeof(*rev);
1230 ccw->cda = (__u32)(unsigned long)rev;
1231
1232 vcdev->revision = VIRTIO_CCW_REV_MAX;
1233 do {
1234 rev->revision = vcdev->revision;
1235 /* none of our supported revisions carry payload */
1236 rev->length = 0;
1237 ret = ccw_io_helper(vcdev, ccw,
1238 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1239 if (ret == -EOPNOTSUPP) {
1240 if (vcdev->revision == 0)
1241 /*
1242 * The host device does not support setting
1243 * the revision: let's operate it in legacy
1244 * mode.
1245 */
1246 ret = 0;
1247 else
1248 vcdev->revision--;
1249 }
1250 } while (ret == -EOPNOTSUPP);
1251
1252 kfree(ccw);
1253 kfree(rev);
1254 return ret;
1255}
7e64e059 1256
7e64e059
CH
1257static int virtio_ccw_online(struct ccw_device *cdev)
1258{
1259 int ret;
1260 struct virtio_ccw_device *vcdev;
2e021043 1261 unsigned long flags;
7e64e059
CH
1262
1263 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1264 if (!vcdev) {
1265 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1266 ret = -ENOMEM;
1267 goto out_free;
1268 }
7e64e059
CH
1269 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1270 GFP_DMA | GFP_KERNEL);
1271 if (!vcdev->config_block) {
1272 ret = -ENOMEM;
1273 goto out_free;
1274 }
73fa21ea
CH
1275 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1276 if (!vcdev->status) {
7e64e059
CH
1277 ret = -ENOMEM;
1278 goto out_free;
1279 }
1280
96b14536
CH
1281 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1282
7e64e059
CH
1283 vcdev->vdev.dev.parent = &cdev->dev;
1284 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1285 vcdev->vdev.config = &virtio_ccw_config_ops;
1286 vcdev->cdev = cdev;
1287 init_waitqueue_head(&vcdev->wait_q);
1288 INIT_LIST_HEAD(&vcdev->virtqueues);
1289 spin_lock_init(&vcdev->lock);
78b1a52e 1290 mutex_init(&vcdev->io_lock);
7e64e059 1291
2e021043 1292 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
7e64e059 1293 dev_set_drvdata(&cdev->dev, vcdev);
2e021043 1294 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
7e64e059
CH
1295 vcdev->vdev.id.vendor = cdev->id.cu_type;
1296 vcdev->vdev.id.device = cdev->id.cu_model;
6bb2c835 1297
2003a7a5
MT
1298 ret = virtio_ccw_set_transport_rev(vcdev);
1299 if (ret)
1300 goto out_free;
6bb2c835 1301
7e64e059
CH
1302 ret = register_virtio_device(&vcdev->vdev);
1303 if (ret) {
1304 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1305 ret);
1306 goto out_put;
1307 }
1308 return 0;
1309out_put:
2e021043 1310 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
7e64e059 1311 dev_set_drvdata(&cdev->dev, NULL);
2e021043 1312 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
7e64e059
CH
1313 put_device(&vcdev->vdev.dev);
1314 return ret;
1315out_free:
1316 if (vcdev) {
73fa21ea 1317 kfree(vcdev->status);
7e64e059 1318 kfree(vcdev->config_block);
7e64e059
CH
1319 }
1320 kfree(vcdev);
1321 return ret;
1322}
1323
1324static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1325{
e75279c4
HG
1326 int rc;
1327 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1328
1329 /*
1330 * Make sure vcdev is set
1331 * i.e. set_offline/remove callback not already running
1332 */
1333 if (!vcdev)
1334 return NOTIFY_DONE;
1335
1336 switch (event) {
1337 case CIO_GONE:
1338 vcdev->device_lost = true;
1339 rc = NOTIFY_DONE;
1340 break;
fa08a3b4
CB
1341 case CIO_OPER:
1342 rc = NOTIFY_OK;
1343 break;
e75279c4
HG
1344 default:
1345 rc = NOTIFY_DONE;
1346 break;
1347 }
1348 return rc;
7e64e059
CH
1349}
1350
1351static struct ccw_device_id virtio_ids[] = {
1352 { CCW_DEVICE(0x3832, 0) },
1353 {},
1354};
7e64e059 1355
fa08a3b4
CB
1356#ifdef CONFIG_PM_SLEEP
1357static int virtio_ccw_freeze(struct ccw_device *cdev)
1358{
1359 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1360
1361 return virtio_device_freeze(&vcdev->vdev);
1362}
1363
1364static int virtio_ccw_restore(struct ccw_device *cdev)
1365{
1366 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1367 int ret;
1368
1369 ret = virtio_ccw_set_transport_rev(vcdev);
1370 if (ret)
1371 return ret;
1372
1373 return virtio_device_restore(&vcdev->vdev);
1374}
1375#endif
1376
7e64e059
CH
1377static struct ccw_driver virtio_ccw_driver = {
1378 .driver = {
1379 .owner = THIS_MODULE,
1380 .name = "virtio_ccw",
1381 },
1382 .ids = virtio_ids,
1383 .probe = virtio_ccw_probe,
1384 .remove = virtio_ccw_remove,
1385 .set_offline = virtio_ccw_offline,
1386 .set_online = virtio_ccw_online,
1387 .notify = virtio_ccw_cio_notify,
89f88337 1388 .int_class = IRQIO_VIR,
fa08a3b4
CB
1389#ifdef CONFIG_PM_SLEEP
1390 .freeze = virtio_ccw_freeze,
1391 .thaw = virtio_ccw_restore,
1392 .restore = virtio_ccw_restore,
1393#endif
7e64e059
CH
1394};
1395
1396static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1397 int max_digit, int max_val)
1398{
1399 int diff;
1400
1401 diff = 0;
1402 *val = 0;
1403
1404 while (diff <= max_digit) {
1405 int value = hex_to_bin(**cp);
1406
1407 if (value < 0)
1408 break;
1409 *val = *val * 16 + value;
1410 (*cp)++;
1411 diff++;
1412 }
1413
1414 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1415 return 1;
1416
1417 return 0;
1418}
1419
1420static int __init parse_busid(char *str, unsigned int *cssid,
1421 unsigned int *ssid, unsigned int *devno)
1422{
1423 char *str_work;
1424 int rc, ret;
1425
1426 rc = 1;
1427
1428 if (*str == '\0')
1429 goto out;
1430
1431 str_work = str;
1432 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1433 if (ret || (str_work[0] != '.'))
1434 goto out;
1435 str_work++;
1436 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1437 if (ret || (str_work[0] != '.'))
1438 goto out;
1439 str_work++;
1440 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1441 if (ret || (str_work[0] != '\0'))
1442 goto out;
1443
1444 rc = 0;
1445out:
1446 return rc;
1447}
1448
1449static void __init no_auto_parse(void)
1450{
1451 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1452 char *parm, *str;
1453 int rc;
1454
1455 str = no_auto;
1456 while ((parm = strsep(&str, ","))) {
1457 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1458 &from_ssid, &from);
1459 if (rc)
1460 continue;
1461 if (parm != NULL) {
1462 rc = parse_busid(parm, &to_cssid,
1463 &to_ssid, &to);
1464 if ((from_ssid > to_ssid) ||
1465 ((from_ssid == to_ssid) && (from > to)))
1466 rc = -EINVAL;
1467 } else {
1468 to_cssid = from_cssid;
1469 to_ssid = from_ssid;
1470 to = from;
1471 }
1472 if (rc)
1473 continue;
1474 while ((from_ssid < to_ssid) ||
1475 ((from_ssid == to_ssid) && (from <= to))) {
1476 set_bit(from, devs_no_auto[from_ssid]);
1477 from++;
1478 if (from > __MAX_SUBCHANNEL) {
1479 from_ssid++;
1480 from = 0;
1481 }
1482 }
1483 }
1484}
1485
1486static int __init virtio_ccw_init(void)
1487{
1488 /* parse no_auto string before we do anything further */
1489 no_auto_parse();
1490 return ccw_driver_register(&virtio_ccw_driver);
1491}
acc50ec7 1492device_initcall(virtio_ccw_init);