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