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