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
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 /* we make cdev->dev.dma_parms point to this */
62 struct device_dma_parameters dma_parms;
63 __u32 curr_io;
64 int err;
65 unsigned int revision; /* Transport revision */
66 wait_queue_head_t wait_q;
67 spinlock_t lock;
68 rwlock_t irq_lock;
69 struct mutex io_lock; /* Serializes I/O requests */
70 struct list_head virtqueues;
71 bool is_thinint;
72 bool going_away;
73 bool device_lost;
74 unsigned int config_ready;
75 void *airq_info;
76 struct vcdev_dma_area *dma_area;
77 dma32_t dma_area_addr;
78};
79
80static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
81{
82 return &vcdev->dma_area->indicators;
83}
84
85static inline unsigned long *indicators2(struct virtio_ccw_device *vcdev)
86{
87 return &vcdev->dma_area->indicators2;
88}
89
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
120struct vq_info_block_legacy {
121 dma64_t queue;
122 __u32 align;
123 __u16 index;
124 __u16 num;
125} __packed;
126
127struct vq_info_block {
128 dma64_t desc;
129 __u32 res0;
130 __u16 index;
131 __u16 num;
132 dma64_t avail;
133 dma64_t used;
134} __packed;
135
136struct virtio_feature_desc {
137 __le32 features;
138 __u8 index;
139} __packed;
140
141struct virtio_thinint_area {
142 dma64_t summary_indicator;
143 dma64_t indicator;
144 u64 bit_nr;
145 u8 isc;
146} __packed;
147
148struct virtio_rev_info {
149 __u16 revision;
150 __u16 length;
151 __u8 data[];
152};
153
154/* the highest virtio-ccw revision we support */
155#define VIRTIO_CCW_REV_MAX 2
156
157struct virtio_ccw_vq_info {
158 struct virtqueue *vq;
159 dma32_t info_block_addr;
160 int num;
161 union {
162 struct vq_info_block s;
163 struct vq_info_block_legacy l;
164 } *info_block;
165 int bit_nr;
166 struct list_head node;
167 long cookie;
168};
169
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;
179 u8 summary_indicator_idx;
180 struct airq_struct airq;
181 struct airq_iv *aiv;
182};
183static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
184static DEFINE_MUTEX(airq_areas_lock);
185
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}
192
193static inline dma64_t get_summary_indicator_dma(struct airq_info *info)
194{
195 return virt_to_dma64(get_summary_indicator(info));
196}
197
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
208#define CCW_CMD_READ_STATUS 0x72
209#define CCW_CMD_SET_IND_ADAPTER 0x73
210#define CCW_CMD_SET_VIRTIO_REV 0x83
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
222#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
223#define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
224#define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
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
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
247static void virtio_airq_handler(struct airq_struct *airq,
248 struct tpi_info *tpi_info)
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 }
262 *(get_summary_indicator(info)) = 0;
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
274static struct airq_info *new_airq_info(int index)
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);
283 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR
284 | AIRQ_IV_CACHELINE, NULL);
285 if (!info->aiv) {
286 kfree(info);
287 return NULL;
288 }
289 info->airq.handler = virtio_airq_handler;
290 info->summary_indicator_idx = index;
291 info->airq.lsi_ptr = get_summary_indicator(info);
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
302static unsigned long *get_airq_indicator(struct virtqueue *vqs[], int nvqs,
303 u64 *first, void **airq_info)
304{
305 int i, j, queue_idx, highest_queue_idx = -1;
306 struct airq_info *info;
307 unsigned long *indicator_addr = NULL;
308 unsigned long bit, flags;
309
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
316 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
317 mutex_lock(&airq_areas_lock);
318 if (!airq_areas[i])
319 airq_areas[i] = new_airq_info(i);
320 info = airq_areas[i];
321 mutex_unlock(&airq_areas_lock);
322 if (!info)
323 return NULL;
324 write_lock_irqsave(&info->lock, flags);
325 bit = airq_iv_alloc(info->aiv, highest_queue_idx + 1);
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;
333 indicator_addr = info->aiv->vector;
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++,
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
349 if (!vcdev->airq_info)
350 return;
351 list_for_each_entry(info, &vcdev->virtqueues, node)
352 drop_airq_indicator(info->vq, vcdev->airq_info);
353}
354
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
369static int ccw_io_helper(struct virtio_ccw_device *vcdev,
370 struct ccw1 *ccw, __u32 intparm)
371{
372 int ret;
373 unsigned long flags;
374 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
375
376 mutex_lock(&vcdev->io_lock);
377 do {
378 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
379 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
380 if (!ret) {
381 if (!vcdev->curr_io)
382 vcdev->err = 0;
383 vcdev->curr_io |= flag;
384 }
385 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
386 cpu_relax();
387 } while (ret == -EBUSY);
388 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
389 ret = ret ? ret : vcdev->err;
390 mutex_unlock(&vcdev->io_lock);
391 return ret;
392}
393
394static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
395 struct ccw1 *ccw)
396{
397 int ret;
398 struct virtio_thinint_area *thinint_area = NULL;
399 struct airq_info *airq_info = vcdev->airq_info;
400 dma64_t *indicatorp = NULL;
401
402 if (vcdev->is_thinint) {
403 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
404 sizeof(*thinint_area),
405 &ccw->cda);
406 if (!thinint_area)
407 return;
408 thinint_area->summary_indicator =
409 get_summary_indicator_dma(airq_info);
410 thinint_area->isc = VIRTIO_AIRQ_ISC;
411 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
412 ccw->count = sizeof(*thinint_area);
413 } else {
414 /* payload is the address of the indicators */
415 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
416 sizeof(*indicatorp),
417 &ccw->cda);
418 if (!indicatorp)
419 return;
420 *indicatorp = 0;
421 ccw->cmd_code = CCW_CMD_SET_IND;
422 ccw->count = sizeof(*indicatorp);
423 }
424 /* Deregister indicators from host. */
425 *indicators(vcdev) = 0;
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);
436 ccw_device_dma_free(vcdev->cdev, indicatorp, sizeof(*indicatorp));
437 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
438}
439
440static inline bool virtio_ccw_do_kvm_notify(struct virtqueue *vq, u32 data)
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);
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),
451 data, info->cookie);
452 if (info->cookie < 0)
453 return false;
454 return true;
455}
456
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
467static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
468 struct ccw1 *ccw, int index)
469{
470 int ret;
471
472 vcdev->dma_area->config_block.index = index;
473 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
474 ccw->flags = 0;
475 ccw->count = sizeof(struct vq_config_block);
476 ccw->cda = config_block_dma(vcdev);
477 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
478 if (ret)
479 return ret;
480 return vcdev->dma_area->config_block.num ?: -ENOENT;
481}
482
483static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
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;
488 int ret;
489 unsigned int index = vq->index;
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. */
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 }
511 ccw->cmd_code = CCW_CMD_SET_VQ;
512 ccw->flags = 0;
513 ccw->cda = info->info_block_addr;
514 ret = ccw_io_helper(vcdev, ccw,
515 VIRTIO_CCW_DOING_SET_VQ | index);
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))
521 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
522 ret, index);
523
524 vring_del_virtqueue(vq);
525 ccw_device_dma_free(vcdev->cdev, info->info_block,
526 sizeof(*info->info_block));
527 kfree(info);
528}
529
530static void virtio_ccw_del_vqs(struct virtio_device *vdev)
531{
532 struct virtqueue *vq, *n;
533 struct ccw1 *ccw;
534 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
535
536 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
537 if (!ccw)
538 return;
539
540 virtio_ccw_drop_indicator(vcdev, ccw);
541
542 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
543 virtio_ccw_del_vq(vq, ccw);
544
545 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
546}
547
548static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
549 int i, vq_callback_t *callback,
550 const char *name, bool ctx,
551 struct ccw1 *ccw)
552{
553 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
554 bool (*notify)(struct virtqueue *vq);
555 int err;
556 struct virtqueue *vq = NULL;
557 struct virtio_ccw_vq_info *info;
558 u64 queue;
559 unsigned long flags;
560 bool may_reduce;
561
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
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 }
574 info->info_block = ccw_device_dma_zalloc(vcdev->cdev,
575 sizeof(*info->info_block),
576 &info->info_block_addr);
577 if (!info->info_block) {
578 dev_warn(&vcdev->cdev->dev, "no info block\n");
579 err = -ENOMEM;
580 goto out_err;
581 }
582 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
583 if (info->num < 0) {
584 err = info->num;
585 goto out_err;
586 }
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,
590 notify, callback, name);
591
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;
596 goto out_err;
597 }
598
599 vq->num_max = info->num;
600
601 /* it may have been reduced */
602 info->num = virtqueue_get_vring_size(vq);
603
604 /* Register it with the host. */
605 queue = virtqueue_get_desc_addr(vq);
606 if (vcdev->revision == 0) {
607 info->info_block->l.queue = u64_to_dma64(queue);
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 {
613 info->info_block->s.desc = u64_to_dma64(queue);
614 info->info_block->s.index = i;
615 info->info_block->s.num = info->num;
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));
618 ccw->count = sizeof(info->info_block->s);
619 }
620 ccw->cmd_code = CCW_CMD_SET_VQ;
621 ccw->flags = 0;
622 ccw->cda = info->info_block_addr;
623 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
624 if (err) {
625 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
626 goto out_err;
627 }
628
629 info->vq = vq;
630 vq->priv = info;
631
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:
640 if (vq)
641 vring_del_virtqueue(vq);
642 if (info) {
643 ccw_device_dma_free(vcdev->cdev, info->info_block,
644 sizeof(*info->info_block));
645 }
646 kfree(info);
647 return ERR_PTR(err);
648}
649
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;
656 unsigned long *indicator_addr;
657 struct airq_info *info;
658
659 thinint_area = ccw_device_dma_zalloc(vcdev->cdev,
660 sizeof(*thinint_area),
661 &ccw->cda);
662 if (!thinint_area) {
663 ret = -ENOMEM;
664 goto out;
665 }
666 /* Try to get an indicator. */
667 indicator_addr = get_airq_indicator(vqs, nvqs,
668 &thinint_area->bit_nr,
669 &vcdev->airq_info);
670 if (!indicator_addr) {
671 ret = -ENOSPC;
672 goto out;
673 }
674 thinint_area->indicator = virt_to_dma64(indicator_addr);
675 info = vcdev->airq_info;
676 thinint_area->summary_indicator = get_summary_indicator_dma(info);
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);
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:
696 ccw_device_dma_free(vcdev->cdev, thinint_area, sizeof(*thinint_area));
697 return ret;
698}
699
700static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
701 struct virtqueue *vqs[],
702 struct virtqueue_info vqs_info[],
703 struct irq_affinity *desc)
704{
705 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
706 dma64_t *indicatorp = NULL;
707 int ret, i, queue_idx = 0;
708 struct ccw1 *ccw;
709 dma32_t indicatorp_dma = 0;
710
711 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
712 if (!ccw)
713 return -ENOMEM;
714
715 for (i = 0; i < nvqs; ++i) {
716 struct virtqueue_info *vqi = &vqs_info[i];
717
718 if (!vqi->name) {
719 vqs[i] = NULL;
720 continue;
721 }
722
723 vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, vqi->callback,
724 vqi->name, vqi->ctx, ccw);
725 if (IS_ERR(vqs[i])) {
726 ret = PTR_ERR(vqs[i]);
727 vqs[i] = NULL;
728 goto out;
729 }
730 }
731 ret = -ENOMEM;
732 /*
733 * We need a data area under 2G to communicate. Our payload is
734 * the address of the indicators.
735 */
736 indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
737 sizeof(*indicatorp),
738 &indicatorp_dma);
739 if (!indicatorp)
740 goto out;
741 *indicatorp = indicators_dma(vcdev);
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 */
746 vcdev->is_thinint = false;
747 }
748 ccw->cda = indicatorp_dma;
749 if (!vcdev->is_thinint) {
750 /* Register queue indicators with host. */
751 *indicators(vcdev) = 0;
752 ccw->cmd_code = CCW_CMD_SET_IND;
753 ccw->flags = 0;
754 ccw->count = sizeof(*indicatorp);
755 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
756 if (ret)
757 goto out;
758 }
759 /* Register indicators2 with host for config changes */
760 *indicatorp = indicators2_dma(vcdev);
761 *indicators2(vcdev) = 0;
762 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
763 ccw->flags = 0;
764 ccw->count = sizeof(*indicatorp);
765 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
766 if (ret)
767 goto out;
768
769 if (indicatorp)
770 ccw_device_dma_free(vcdev->cdev, indicatorp,
771 sizeof(*indicatorp));
772 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
773 return 0;
774out:
775 if (indicatorp)
776 ccw_device_dma_free(vcdev->cdev, indicatorp,
777 sizeof(*indicatorp));
778 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
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);
786 struct ccw1 *ccw;
787
788 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
789 if (!ccw)
790 return;
791
792 /* Zero status bits. */
793 vcdev->dma_area->status = 0;
794
795 /* Send a reset ccw on device. */
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);
801 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
802}
803
804static u64 virtio_ccw_get_features(struct virtio_device *vdev)
805{
806 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
807 struct virtio_feature_desc *features;
808 int ret;
809 u64 rc;
810 struct ccw1 *ccw;
811
812 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
813 if (!ccw)
814 return 0;
815
816 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
817 &ccw->cda);
818 if (!features) {
819 rc = 0;
820 goto out_free;
821 }
822 /* Read the feature bits from the host. */
823 features->index = 0;
824 ccw->cmd_code = CCW_CMD_READ_FEAT;
825 ccw->flags = 0;
826 ccw->count = sizeof(*features);
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);
834
835 if (vcdev->revision == 0)
836 goto out_free;
837
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);
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
847out_free:
848 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
849 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
850 return rc;
851}
852
853static void ccw_transport_features(struct virtio_device *vdev)
854{
855 /*
856 * Currently nothing to do here.
857 */
858}
859
860static int virtio_ccw_finalize_features(struct virtio_device *vdev)
861{
862 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
863 struct virtio_feature_desc *features;
864 struct ccw1 *ccw;
865 int ret;
866
867 if (vcdev->revision >= 1 &&
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
874 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
875 if (!ccw)
876 return -ENOMEM;
877
878 features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
879 &ccw->cda);
880 if (!features) {
881 ret = -ENOMEM;
882 goto out_free;
883 }
884 /* Give virtio_ring a chance to accept features. */
885 vring_transport_features(vdev);
886
887 /* Give virtio_ccw a chance to accept features. */
888 ccw_transport_features(vdev);
889
890 features->index = 0;
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);
896 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
897 if (ret)
898 goto out_free;
899
900 if (vcdev->revision == 0)
901 goto out_free;
902
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. */
906 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
907 ccw->flags = 0;
908 ccw->count = sizeof(*features);
909 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
910
911out_free:
912 ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
913 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
914
915 return ret;
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;
923 struct ccw1 *ccw;
924 void *config_area;
925 unsigned long flags;
926
927 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
928 if (!ccw)
929 return;
930
931 config_area = ccw_device_dma_zalloc(vcdev->cdev,
932 VIRTIO_CCW_CONFIG_SIZE,
933 &ccw->cda);
934 if (!config_area)
935 goto out_free;
936
937 /* Read the config area from the host. */
938 ccw->cmd_code = CCW_CMD_READ_CONF;
939 ccw->flags = 0;
940 ccw->count = offset + len;
941 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
942 if (ret)
943 goto out_free;
944
945 spin_lock_irqsave(&vcdev->lock, flags);
946 memcpy(vcdev->config, config_area, offset + len);
947 if (vcdev->config_ready < offset + len)
948 vcdev->config_ready = offset + len;
949 spin_unlock_irqrestore(&vcdev->lock, flags);
950 if (buf)
951 memcpy(buf, config_area + offset, len);
952
953out_free:
954 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
955 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
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);
963 struct ccw1 *ccw;
964 void *config_area;
965 unsigned long flags;
966
967 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
968 if (!ccw)
969 return;
970
971 config_area = ccw_device_dma_zalloc(vcdev->cdev,
972 VIRTIO_CCW_CONFIG_SIZE,
973 &ccw->cda);
974 if (!config_area)
975 goto out_free;
976
977 /* Make sure we don't overwrite fields. */
978 if (vcdev->config_ready < offset)
979 virtio_ccw_get_config(vdev, 0, NULL, offset);
980 spin_lock_irqsave(&vcdev->lock, flags);
981 memcpy(&vcdev->config[offset], buf, len);
982 /* Write the config area to the host. */
983 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
984 spin_unlock_irqrestore(&vcdev->lock, flags);
985 ccw->cmd_code = CCW_CMD_WRITE_CONF;
986 ccw->flags = 0;
987 ccw->count = offset + len;
988 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
989
990out_free:
991 ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
992 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
993}
994
995static u8 virtio_ccw_get_status(struct virtio_device *vdev)
996{
997 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
998 u8 old_status = vcdev->dma_area->status;
999 struct ccw1 *ccw;
1000
1001 if (vcdev->revision < 2)
1002 return vcdev->dma_area->status;
1003
1004 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1005 if (!ccw)
1006 return old_status;
1007
1008 ccw->cmd_code = CCW_CMD_READ_STATUS;
1009 ccw->flags = 0;
1010 ccw->count = sizeof(vcdev->dma_area->status);
1011 ccw->cda = status_dma(vcdev);
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
1016 * handler anyway), vcdev->dma_area->status was not overwritten and we just
1017 * return the old status, which is fine.
1018*/
1019 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1020
1021 return vcdev->dma_area->status;
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);
1027 u8 old_status = vcdev->dma_area->status;
1028 struct ccw1 *ccw;
1029 int ret;
1030
1031 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1032 if (!ccw)
1033 return;
1034
1035 /* Write the status to the host. */
1036 vcdev->dma_area->status = status;
1037 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
1038 ccw->flags = 0;
1039 ccw->count = sizeof(status);
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 */
1044 ccw->cda = status_dma(vcdev);
1045 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
1046 /* Write failed? We assume status is unchanged. */
1047 if (ret)
1048 vcdev->dma_area->status = old_status;
1049 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1050}
1051
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
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
1083static const struct virtio_config_ops virtio_ccw_config_ops = {
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,
1091 .find_vqs = virtio_ccw_find_vqs,
1092 .del_vqs = virtio_ccw_del_vqs,
1093 .bus_name = virtio_ccw_bus_name,
1094 .synchronize_cbs = virtio_ccw_synchronize_cbs,
1095};
1096
1097
1098/*
1099 * ccw bus driver related functions
1100 */
1101
1102static void virtio_ccw_release_dev(struct device *_d)
1103{
1104 struct virtio_device *dev = dev_to_virtio(_d);
1105 struct virtio_ccw_device *vcdev = to_vc_device(dev);
1106
1107 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1108 sizeof(*vcdev->dma_area));
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) {
1133 if (info->vq->index == index) {
1134 vq = info->vq;
1135 break;
1136 }
1137 }
1138 spin_unlock_irqrestore(&vcdev->lock, flags);
1139 return vq;
1140}
1141
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:
1152 case VIRTIO_CCW_DOING_READ_STATUS:
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
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;
1181
1182 if (!vcdev)
1183 return;
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 }
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 }
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 }
1205 virtio_ccw_check_activity(vcdev, activity);
1206#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1207 /*
1208 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1209 * disabled here.
1210 */
1211 read_lock(&vcdev->irq_lock);
1212#endif
1213 for_each_set_bit(i, indicators(vcdev),
1214 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1215 /* The bit clear must happen before the vring kick. */
1216 clear_bit(i, indicators(vcdev));
1217 barrier();
1218 vq = virtio_ccw_vq_by_ind(vcdev, i);
1219 vring_interrupt(0, vq);
1220 }
1221#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1222 read_unlock(&vcdev->irq_lock);
1223#endif
1224 if (test_bit(0, indicators2(vcdev))) {
1225 virtio_config_changed(&vcdev->vdev);
1226 clear_bit(0, indicators2(vcdev));
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
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);
1279 if (!vcdev || vcdev->going_away) {
1280 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1281 return NULL;
1282 }
1283 vcdev->going_away = true;
1284 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1285 return vcdev;
1286}
1287
1288static void virtio_ccw_remove(struct ccw_device *cdev)
1289{
1290 unsigned long flags;
1291 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1292
1293 if (vcdev && cdev->online) {
1294 if (vcdev->device_lost)
1295 virtio_break_device(&vcdev->vdev);
1296 unregister_virtio_device(&vcdev->vdev);
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 }
1301 cdev->handler = NULL;
1302}
1303
1304static int virtio_ccw_offline(struct ccw_device *cdev)
1305{
1306 unsigned long flags;
1307 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1308
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);
1316 cdev->dev.dma_parms = NULL;
1317 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1318 return 0;
1319}
1320
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
1327 ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1328 if (!ccw)
1329 return -ENOMEM;
1330 rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev), &ccw->cda);
1331 if (!rev) {
1332 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
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);
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
1361 ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1362 ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1363 return ret;
1364}
1365
1366static int virtio_ccw_online(struct ccw_device *cdev)
1367{
1368 int ret;
1369 struct virtio_ccw_device *vcdev;
1370 unsigned long flags;
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 }
1378 vcdev->vdev.dev.parent = &cdev->dev;
1379 vcdev->cdev = cdev;
1380 cdev->dev.dma_parms = &vcdev->dma_parms;
1381 vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1382 sizeof(*vcdev->dma_area),
1383 &vcdev->dma_area_addr);
1384 if (!vcdev->dma_area) {
1385 ret = -ENOMEM;
1386 goto out_free;
1387 }
1388
1389 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1390
1391 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1392 vcdev->vdev.config = &virtio_ccw_config_ops;
1393 init_waitqueue_head(&vcdev->wait_q);
1394 INIT_LIST_HEAD(&vcdev->virtqueues);
1395 spin_lock_init(&vcdev->lock);
1396 rwlock_init(&vcdev->irq_lock);
1397 mutex_init(&vcdev->io_lock);
1398
1399 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1400 dev_set_drvdata(&cdev->dev, vcdev);
1401 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1402 vcdev->vdev.id.vendor = cdev->id.cu_type;
1403 vcdev->vdev.id.device = cdev->id.cu_model;
1404
1405 ret = virtio_ccw_set_transport_rev(vcdev);
1406 if (ret)
1407 goto out_free;
1408
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:
1417 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1418 dev_set_drvdata(&cdev->dev, NULL);
1419 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1420 put_device(&vcdev->vdev.dev);
1421 return ret;
1422out_free:
1423 if (vcdev) {
1424 ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1425 sizeof(*vcdev->dma_area));
1426 }
1427 kfree(vcdev);
1428 return ret;
1429}
1430
1431static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1432{
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;
1448 case CIO_OPER:
1449 rc = NOTIFY_OK;
1450 break;
1451 default:
1452 rc = NOTIFY_DONE;
1453 break;
1454 }
1455 return rc;
1456}
1457
1458static struct ccw_device_id virtio_ids[] = {
1459 { CCW_DEVICE(0x3832, 0) },
1460 {},
1461};
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,
1474 .int_class = IRQIO_VIR,
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{
1569 int rc;
1570
1571 /* parse no_auto string before we do anything further */
1572 no_auto_parse();
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;
1581}
1582device_initcall(virtio_ccw_init);