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