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