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