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