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