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