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