xref: /linux/drivers/s390/virtio/virtio_ccw.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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 
indicators(struct virtio_ccw_device * vcdev)78 static inline unsigned long *indicators(struct virtio_ccw_device *vcdev)
79 {
80 	return &vcdev->dma_area->indicators;
81 }
82 
indicators2(struct virtio_ccw_device * vcdev)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 */
indicators_dma(struct virtio_ccw_device * vcdev)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 */
indicators2_dma(struct virtio_ccw_device * vcdev)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 
config_block_dma(struct virtio_ccw_device * vcdev)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 
status_dma(struct virtio_ccw_device * vcdev)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 
get_summary_indicator(struct airq_info * info)186 static inline u8 *get_summary_indicator(struct airq_info *info)
187 {
188 	return summary_indicators + info->summary_indicator_idx;
189 }
190 
get_summary_indicator_dma(struct airq_info * info)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 
to_vc_device(struct virtio_device * vdev)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 
drop_airq_indicator(struct virtqueue * vq,struct airq_info * info)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 
virtio_airq_handler(struct airq_struct * airq,struct tpi_info * tpi_info)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 
new_airq_info(int index)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 
get_airq_indicator(struct virtqueue * vqs[],int nvqs,u64 * first,void ** airq_info)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 
virtio_ccw_drop_indicators(struct virtio_ccw_device * vcdev)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 
doing_io(struct virtio_ccw_device * vcdev,__u32 flag)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 
ccw_io_helper(struct virtio_ccw_device * vcdev,struct ccw1 * ccw,__u32 intparm)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 
virtio_ccw_drop_indicator(struct virtio_ccw_device * vcdev,struct ccw1 * ccw)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 
virtio_ccw_do_kvm_notify(struct virtqueue * vq,u32 data)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 
virtio_ccw_kvm_notify(struct virtqueue * vq)447 static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
448 {
449 	return virtio_ccw_do_kvm_notify(vq, vq->index);
450 }
451 
virtio_ccw_kvm_notify_with_data(struct virtqueue * vq)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 
virtio_ccw_read_vq_conf(struct virtio_ccw_device * vcdev,struct ccw1 * ccw,int index)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 
virtio_ccw_del_vq(struct virtqueue * vq,struct ccw1 * ccw)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 
virtio_ccw_del_vqs(struct virtio_device * vdev)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 
virtio_ccw_setup_vq(struct virtio_device * vdev,int i,vq_callback_t * callback,const char * name,bool ctx,struct ccw1 * ccw)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 
virtio_ccw_register_adapter_ind(struct virtio_ccw_device * vcdev,struct virtqueue * vqs[],int nvqs,struct ccw1 * ccw)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 
virtio_ccw_find_vqs(struct virtio_device * vdev,unsigned nvqs,struct virtqueue * vqs[],struct virtqueue_info vqs_info[],struct irq_affinity * desc)690 static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
691 			       struct virtqueue *vqs[],
692 			       struct virtqueue_info vqs_info[],
693 			       struct irq_affinity *desc)
694 {
695 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
696 	dma64_t *indicatorp = NULL;
697 	int ret, i, queue_idx = 0;
698 	struct ccw1 *ccw;
699 	dma32_t indicatorp_dma = 0;
700 
701 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
702 	if (!ccw)
703 		return -ENOMEM;
704 
705 	for (i = 0; i < nvqs; ++i) {
706 		struct virtqueue_info *vqi = &vqs_info[i];
707 
708 		if (!vqi->name) {
709 			vqs[i] = NULL;
710 			continue;
711 		}
712 
713 		vqs[i] = virtio_ccw_setup_vq(vdev, queue_idx++, vqi->callback,
714 					     vqi->name, vqi->ctx, ccw);
715 		if (IS_ERR(vqs[i])) {
716 			ret = PTR_ERR(vqs[i]);
717 			vqs[i] = NULL;
718 			goto out;
719 		}
720 	}
721 	ret = -ENOMEM;
722 	/*
723 	 * We need a data area under 2G to communicate. Our payload is
724 	 * the address of the indicators.
725 	*/
726 	indicatorp = ccw_device_dma_zalloc(vcdev->cdev,
727 					   sizeof(*indicatorp),
728 					   &indicatorp_dma);
729 	if (!indicatorp)
730 		goto out;
731 	*indicatorp = indicators_dma(vcdev);
732 	if (vcdev->is_thinint) {
733 		ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
734 		if (ret)
735 			/* no error, just fall back to legacy interrupts */
736 			vcdev->is_thinint = false;
737 	}
738 	ccw->cda = indicatorp_dma;
739 	if (!vcdev->is_thinint) {
740 		/* Register queue indicators with host. */
741 		*indicators(vcdev) = 0;
742 		ccw->cmd_code = CCW_CMD_SET_IND;
743 		ccw->flags = 0;
744 		ccw->count = sizeof(*indicatorp);
745 		ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
746 		if (ret)
747 			goto out;
748 	}
749 	/* Register indicators2 with host for config changes */
750 	*indicatorp = indicators2_dma(vcdev);
751 	*indicators2(vcdev) = 0;
752 	ccw->cmd_code = CCW_CMD_SET_CONF_IND;
753 	ccw->flags = 0;
754 	ccw->count = sizeof(*indicatorp);
755 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
756 	if (ret)
757 		goto out;
758 
759 	if (indicatorp)
760 		ccw_device_dma_free(vcdev->cdev, indicatorp,
761 				    sizeof(*indicatorp));
762 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
763 	return 0;
764 out:
765 	if (indicatorp)
766 		ccw_device_dma_free(vcdev->cdev, indicatorp,
767 				    sizeof(*indicatorp));
768 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
769 	virtio_ccw_del_vqs(vdev);
770 	return ret;
771 }
772 
virtio_ccw_reset(struct virtio_device * vdev)773 static void virtio_ccw_reset(struct virtio_device *vdev)
774 {
775 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
776 	struct ccw1 *ccw;
777 
778 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
779 	if (!ccw)
780 		return;
781 
782 	/* Zero status bits. */
783 	vcdev->dma_area->status = 0;
784 
785 	/* Send a reset ccw on device. */
786 	ccw->cmd_code = CCW_CMD_VDEV_RESET;
787 	ccw->flags = 0;
788 	ccw->count = 0;
789 	ccw->cda = 0;
790 	ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
791 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
792 }
793 
virtio_ccw_get_features(struct virtio_device * vdev)794 static u64 virtio_ccw_get_features(struct virtio_device *vdev)
795 {
796 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
797 	struct virtio_feature_desc *features;
798 	int ret;
799 	u64 rc;
800 	struct ccw1 *ccw;
801 
802 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
803 	if (!ccw)
804 		return 0;
805 
806 	features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
807 					 &ccw->cda);
808 	if (!features) {
809 		rc = 0;
810 		goto out_free;
811 	}
812 	/* Read the feature bits from the host. */
813 	features->index = 0;
814 	ccw->cmd_code = CCW_CMD_READ_FEAT;
815 	ccw->flags = 0;
816 	ccw->count = sizeof(*features);
817 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
818 	if (ret) {
819 		rc = 0;
820 		goto out_free;
821 	}
822 
823 	rc = le32_to_cpu(features->features);
824 
825 	if (vcdev->revision == 0)
826 		goto out_free;
827 
828 	/* Read second half of the feature bits from the host. */
829 	features->index = 1;
830 	ccw->cmd_code = CCW_CMD_READ_FEAT;
831 	ccw->flags = 0;
832 	ccw->count = sizeof(*features);
833 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
834 	if (ret == 0)
835 		rc |= (u64)le32_to_cpu(features->features) << 32;
836 
837 out_free:
838 	ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
839 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
840 	return rc;
841 }
842 
ccw_transport_features(struct virtio_device * vdev)843 static void ccw_transport_features(struct virtio_device *vdev)
844 {
845 	/*
846 	 * Currently nothing to do here.
847 	 */
848 }
849 
virtio_ccw_finalize_features(struct virtio_device * vdev)850 static int virtio_ccw_finalize_features(struct virtio_device *vdev)
851 {
852 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
853 	struct virtio_feature_desc *features;
854 	struct ccw1 *ccw;
855 	int ret;
856 
857 	if (vcdev->revision >= 1 &&
858 	    !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
859 		dev_err(&vdev->dev, "virtio: device uses revision 1 "
860 			"but does not have VIRTIO_F_VERSION_1\n");
861 		return -EINVAL;
862 	}
863 
864 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
865 	if (!ccw)
866 		return -ENOMEM;
867 
868 	features = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*features),
869 					 &ccw->cda);
870 	if (!features) {
871 		ret = -ENOMEM;
872 		goto out_free;
873 	}
874 	/* Give virtio_ring a chance to accept features. */
875 	vring_transport_features(vdev);
876 
877 	/* Give virtio_ccw a chance to accept features. */
878 	ccw_transport_features(vdev);
879 
880 	features->index = 0;
881 	features->features = cpu_to_le32((u32)vdev->features);
882 	/* Write the first half of the feature bits to the host. */
883 	ccw->cmd_code = CCW_CMD_WRITE_FEAT;
884 	ccw->flags = 0;
885 	ccw->count = sizeof(*features);
886 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
887 	if (ret)
888 		goto out_free;
889 
890 	if (vcdev->revision == 0)
891 		goto out_free;
892 
893 	features->index = 1;
894 	features->features = cpu_to_le32(vdev->features >> 32);
895 	/* Write the second half of the feature bits to the host. */
896 	ccw->cmd_code = CCW_CMD_WRITE_FEAT;
897 	ccw->flags = 0;
898 	ccw->count = sizeof(*features);
899 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
900 
901 out_free:
902 	ccw_device_dma_free(vcdev->cdev, features, sizeof(*features));
903 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
904 
905 	return ret;
906 }
907 
virtio_ccw_get_config(struct virtio_device * vdev,unsigned int offset,void * buf,unsigned len)908 static void virtio_ccw_get_config(struct virtio_device *vdev,
909 				  unsigned int offset, void *buf, unsigned len)
910 {
911 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
912 	int ret;
913 	struct ccw1 *ccw;
914 	void *config_area;
915 	unsigned long flags;
916 
917 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
918 	if (!ccw)
919 		return;
920 
921 	config_area = ccw_device_dma_zalloc(vcdev->cdev,
922 					    VIRTIO_CCW_CONFIG_SIZE,
923 					    &ccw->cda);
924 	if (!config_area)
925 		goto out_free;
926 
927 	/* Read the config area from the host. */
928 	ccw->cmd_code = CCW_CMD_READ_CONF;
929 	ccw->flags = 0;
930 	ccw->count = offset + len;
931 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
932 	if (ret)
933 		goto out_free;
934 
935 	spin_lock_irqsave(&vcdev->lock, flags);
936 	memcpy(vcdev->config, config_area, offset + len);
937 	if (vcdev->config_ready < offset + len)
938 		vcdev->config_ready = offset + len;
939 	spin_unlock_irqrestore(&vcdev->lock, flags);
940 	if (buf)
941 		memcpy(buf, config_area + offset, len);
942 
943 out_free:
944 	ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
945 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
946 }
947 
virtio_ccw_set_config(struct virtio_device * vdev,unsigned int offset,const void * buf,unsigned len)948 static void virtio_ccw_set_config(struct virtio_device *vdev,
949 				  unsigned int offset, const void *buf,
950 				  unsigned len)
951 {
952 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
953 	struct ccw1 *ccw;
954 	void *config_area;
955 	unsigned long flags;
956 
957 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
958 	if (!ccw)
959 		return;
960 
961 	config_area = ccw_device_dma_zalloc(vcdev->cdev,
962 					    VIRTIO_CCW_CONFIG_SIZE,
963 					    &ccw->cda);
964 	if (!config_area)
965 		goto out_free;
966 
967 	/* Make sure we don't overwrite fields. */
968 	if (vcdev->config_ready < offset)
969 		virtio_ccw_get_config(vdev, 0, NULL, offset);
970 	spin_lock_irqsave(&vcdev->lock, flags);
971 	memcpy(&vcdev->config[offset], buf, len);
972 	/* Write the config area to the host. */
973 	memcpy(config_area, vcdev->config, sizeof(vcdev->config));
974 	spin_unlock_irqrestore(&vcdev->lock, flags);
975 	ccw->cmd_code = CCW_CMD_WRITE_CONF;
976 	ccw->flags = 0;
977 	ccw->count = offset + len;
978 	ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
979 
980 out_free:
981 	ccw_device_dma_free(vcdev->cdev, config_area, VIRTIO_CCW_CONFIG_SIZE);
982 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
983 }
984 
virtio_ccw_get_status(struct virtio_device * vdev)985 static u8 virtio_ccw_get_status(struct virtio_device *vdev)
986 {
987 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
988 	u8 old_status = vcdev->dma_area->status;
989 	struct ccw1 *ccw;
990 
991 	if (vcdev->revision < 2)
992 		return vcdev->dma_area->status;
993 
994 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
995 	if (!ccw)
996 		return old_status;
997 
998 	ccw->cmd_code = CCW_CMD_READ_STATUS;
999 	ccw->flags = 0;
1000 	ccw->count = sizeof(vcdev->dma_area->status);
1001 	ccw->cda = status_dma(vcdev);
1002 	ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
1003 /*
1004  * If the channel program failed (should only happen if the device
1005  * was hotunplugged, and then we clean up via the machine check
1006  * handler anyway), vcdev->dma_area->status was not overwritten and we just
1007  * return the old status, which is fine.
1008 */
1009 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1010 
1011 	return vcdev->dma_area->status;
1012 }
1013 
virtio_ccw_set_status(struct virtio_device * vdev,u8 status)1014 static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
1015 {
1016 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1017 	u8 old_status = vcdev->dma_area->status;
1018 	struct ccw1 *ccw;
1019 	int ret;
1020 
1021 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1022 	if (!ccw)
1023 		return;
1024 
1025 	/* Write the status to the host. */
1026 	vcdev->dma_area->status = status;
1027 	ccw->cmd_code = CCW_CMD_WRITE_STATUS;
1028 	ccw->flags = 0;
1029 	ccw->count = sizeof(status);
1030 	/* We use ssch for setting the status which is a serializing
1031 	 * instruction that guarantees the memory writes have
1032 	 * completed before ssch.
1033 	 */
1034 	ccw->cda = status_dma(vcdev);
1035 	ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
1036 	/* Write failed? We assume status is unchanged. */
1037 	if (ret)
1038 		vcdev->dma_area->status = old_status;
1039 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1040 }
1041 
virtio_ccw_bus_name(struct virtio_device * vdev)1042 static const char *virtio_ccw_bus_name(struct virtio_device *vdev)
1043 {
1044 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1045 
1046 	return dev_name(&vcdev->cdev->dev);
1047 }
1048 
virtio_ccw_synchronize_cbs(struct virtio_device * vdev)1049 static void virtio_ccw_synchronize_cbs(struct virtio_device *vdev)
1050 {
1051 	struct virtio_ccw_device *vcdev = to_vc_device(vdev);
1052 	struct airq_info *info = vcdev->airq_info;
1053 
1054 	if (info) {
1055 		/*
1056 		 * This device uses adapter interrupts: synchronize with
1057 		 * vring_interrupt() called by virtio_airq_handler()
1058 		 * via the indicator area lock.
1059 		 */
1060 		write_lock_irq(&info->lock);
1061 		write_unlock_irq(&info->lock);
1062 	} else {
1063 		/* This device uses classic interrupts: synchronize
1064 		 * with vring_interrupt() called by
1065 		 * virtio_ccw_int_handler() via the per-device
1066 		 * irq_lock
1067 		 */
1068 		write_lock_irq(&vcdev->irq_lock);
1069 		write_unlock_irq(&vcdev->irq_lock);
1070 	}
1071 }
1072 
1073 static const struct virtio_config_ops virtio_ccw_config_ops = {
1074 	.get_features = virtio_ccw_get_features,
1075 	.finalize_features = virtio_ccw_finalize_features,
1076 	.get = virtio_ccw_get_config,
1077 	.set = virtio_ccw_set_config,
1078 	.get_status = virtio_ccw_get_status,
1079 	.set_status = virtio_ccw_set_status,
1080 	.reset = virtio_ccw_reset,
1081 	.find_vqs = virtio_ccw_find_vqs,
1082 	.del_vqs = virtio_ccw_del_vqs,
1083 	.bus_name = virtio_ccw_bus_name,
1084 	.synchronize_cbs = virtio_ccw_synchronize_cbs,
1085 };
1086 
1087 
1088 /*
1089  * ccw bus driver related functions
1090  */
1091 
virtio_ccw_release_dev(struct device * _d)1092 static void virtio_ccw_release_dev(struct device *_d)
1093 {
1094 	struct virtio_device *dev = dev_to_virtio(_d);
1095 	struct virtio_ccw_device *vcdev = to_vc_device(dev);
1096 
1097 	ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1098 			    sizeof(*vcdev->dma_area));
1099 	kfree(vcdev);
1100 }
1101 
irb_is_error(struct irb * irb)1102 static int irb_is_error(struct irb *irb)
1103 {
1104 	if (scsw_cstat(&irb->scsw) != 0)
1105 		return 1;
1106 	if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
1107 		return 1;
1108 	if (scsw_cc(&irb->scsw) != 0)
1109 		return 1;
1110 	return 0;
1111 }
1112 
virtio_ccw_vq_by_ind(struct virtio_ccw_device * vcdev,int index)1113 static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
1114 					      int index)
1115 {
1116 	struct virtio_ccw_vq_info *info;
1117 	unsigned long flags;
1118 	struct virtqueue *vq;
1119 
1120 	vq = NULL;
1121 	spin_lock_irqsave(&vcdev->lock, flags);
1122 	list_for_each_entry(info, &vcdev->virtqueues, node) {
1123 		if (info->vq->index == index) {
1124 			vq = info->vq;
1125 			break;
1126 		}
1127 	}
1128 	spin_unlock_irqrestore(&vcdev->lock, flags);
1129 	return vq;
1130 }
1131 
virtio_ccw_check_activity(struct virtio_ccw_device * vcdev,__u32 activity)1132 static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1133 				      __u32 activity)
1134 {
1135 	if (vcdev->curr_io & activity) {
1136 		switch (activity) {
1137 		case VIRTIO_CCW_DOING_READ_FEAT:
1138 		case VIRTIO_CCW_DOING_WRITE_FEAT:
1139 		case VIRTIO_CCW_DOING_READ_CONFIG:
1140 		case VIRTIO_CCW_DOING_WRITE_CONFIG:
1141 		case VIRTIO_CCW_DOING_WRITE_STATUS:
1142 		case VIRTIO_CCW_DOING_READ_STATUS:
1143 		case VIRTIO_CCW_DOING_SET_VQ:
1144 		case VIRTIO_CCW_DOING_SET_IND:
1145 		case VIRTIO_CCW_DOING_SET_CONF_IND:
1146 		case VIRTIO_CCW_DOING_RESET:
1147 		case VIRTIO_CCW_DOING_READ_VQ_CONF:
1148 		case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
1149 		case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
1150 			vcdev->curr_io &= ~activity;
1151 			wake_up(&vcdev->wait_q);
1152 			break;
1153 		default:
1154 			/* don't know what to do... */
1155 			dev_warn(&vcdev->cdev->dev,
1156 				 "Suspicious activity '%08x'\n", activity);
1157 			WARN_ON(1);
1158 			break;
1159 		}
1160 	}
1161 }
1162 
virtio_ccw_int_handler(struct ccw_device * cdev,unsigned long intparm,struct irb * irb)1163 static void virtio_ccw_int_handler(struct ccw_device *cdev,
1164 				   unsigned long intparm,
1165 				   struct irb *irb)
1166 {
1167 	__u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1168 	struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1169 	int i;
1170 	struct virtqueue *vq;
1171 
1172 	if (!vcdev)
1173 		return;
1174 	if (IS_ERR(irb)) {
1175 		vcdev->err = PTR_ERR(irb);
1176 		virtio_ccw_check_activity(vcdev, activity);
1177 		/* Don't poke around indicators, something's wrong. */
1178 		return;
1179 	}
1180 	/* Check if it's a notification from the host. */
1181 	if ((intparm == 0) &&
1182 	    (scsw_stctl(&irb->scsw) ==
1183 	     (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1184 		/* OK */
1185 	}
1186 	if (irb_is_error(irb)) {
1187 		/* Command reject? */
1188 		if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1189 		    (irb->ecw[0] & SNS0_CMD_REJECT))
1190 			vcdev->err = -EOPNOTSUPP;
1191 		else
1192 			/* Map everything else to -EIO. */
1193 			vcdev->err = -EIO;
1194 	}
1195 	virtio_ccw_check_activity(vcdev, activity);
1196 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1197 	/*
1198 	 * Paired with virtio_ccw_synchronize_cbs() and interrupts are
1199 	 * disabled here.
1200 	 */
1201 	read_lock(&vcdev->irq_lock);
1202 #endif
1203 	for_each_set_bit(i, indicators(vcdev),
1204 			 sizeof(*indicators(vcdev)) * BITS_PER_BYTE) {
1205 		/* The bit clear must happen before the vring kick. */
1206 		clear_bit(i, indicators(vcdev));
1207 		barrier();
1208 		vq = virtio_ccw_vq_by_ind(vcdev, i);
1209 		vring_interrupt(0, vq);
1210 	}
1211 #ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION
1212 	read_unlock(&vcdev->irq_lock);
1213 #endif
1214 	if (test_bit(0, indicators2(vcdev))) {
1215 		virtio_config_changed(&vcdev->vdev);
1216 		clear_bit(0, indicators2(vcdev));
1217 	}
1218 }
1219 
1220 /*
1221  * We usually want to autoonline all devices, but give the admin
1222  * a way to exempt devices from this.
1223  */
1224 #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1225 		     (8*sizeof(long)))
1226 static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1227 
1228 static char *no_auto = "";
1229 
1230 module_param(no_auto, charp, 0444);
1231 MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1232 
virtio_ccw_check_autoonline(struct ccw_device * cdev)1233 static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1234 {
1235 	struct ccw_dev_id id;
1236 
1237 	ccw_device_get_id(cdev, &id);
1238 	if (test_bit(id.devno, devs_no_auto[id.ssid]))
1239 		return 0;
1240 	return 1;
1241 }
1242 
virtio_ccw_auto_online(void * data,async_cookie_t cookie)1243 static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1244 {
1245 	struct ccw_device *cdev = data;
1246 	int ret;
1247 
1248 	ret = ccw_device_set_online(cdev);
1249 	if (ret)
1250 		dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1251 }
1252 
virtio_ccw_probe(struct ccw_device * cdev)1253 static int virtio_ccw_probe(struct ccw_device *cdev)
1254 {
1255 	cdev->handler = virtio_ccw_int_handler;
1256 
1257 	if (virtio_ccw_check_autoonline(cdev))
1258 		async_schedule(virtio_ccw_auto_online, cdev);
1259 	return 0;
1260 }
1261 
virtio_grab_drvdata(struct ccw_device * cdev)1262 static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1263 {
1264 	unsigned long flags;
1265 	struct virtio_ccw_device *vcdev;
1266 
1267 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1268 	vcdev = dev_get_drvdata(&cdev->dev);
1269 	if (!vcdev || vcdev->going_away) {
1270 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1271 		return NULL;
1272 	}
1273 	vcdev->going_away = true;
1274 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1275 	return vcdev;
1276 }
1277 
virtio_ccw_remove(struct ccw_device * cdev)1278 static void virtio_ccw_remove(struct ccw_device *cdev)
1279 {
1280 	unsigned long flags;
1281 	struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1282 
1283 	if (vcdev && cdev->online) {
1284 		if (vcdev->device_lost)
1285 			virtio_break_device(&vcdev->vdev);
1286 		unregister_virtio_device(&vcdev->vdev);
1287 		spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1288 		dev_set_drvdata(&cdev->dev, NULL);
1289 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1290 	}
1291 	cdev->handler = NULL;
1292 }
1293 
virtio_ccw_offline(struct ccw_device * cdev)1294 static int virtio_ccw_offline(struct ccw_device *cdev)
1295 {
1296 	unsigned long flags;
1297 	struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
1298 
1299 	if (!vcdev)
1300 		return 0;
1301 	if (vcdev->device_lost)
1302 		virtio_break_device(&vcdev->vdev);
1303 	unregister_virtio_device(&vcdev->vdev);
1304 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1305 	dev_set_drvdata(&cdev->dev, NULL);
1306 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1307 	return 0;
1308 }
1309 
virtio_ccw_set_transport_rev(struct virtio_ccw_device * vcdev)1310 static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1311 {
1312 	struct virtio_rev_info *rev;
1313 	struct ccw1 *ccw;
1314 	int ret;
1315 
1316 	ccw = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*ccw), NULL);
1317 	if (!ccw)
1318 		return -ENOMEM;
1319 	rev = ccw_device_dma_zalloc(vcdev->cdev, sizeof(*rev), &ccw->cda);
1320 	if (!rev) {
1321 		ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1322 		return -ENOMEM;
1323 	}
1324 
1325 	/* Set transport revision */
1326 	ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1327 	ccw->flags = 0;
1328 	ccw->count = sizeof(*rev);
1329 
1330 	vcdev->revision = VIRTIO_CCW_REV_MAX;
1331 	do {
1332 		rev->revision = vcdev->revision;
1333 		/* none of our supported revisions carry payload */
1334 		rev->length = 0;
1335 		ret = ccw_io_helper(vcdev, ccw,
1336 				    VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1337 		if (ret == -EOPNOTSUPP) {
1338 			if (vcdev->revision == 0)
1339 				/*
1340 				 * The host device does not support setting
1341 				 * the revision: let's operate it in legacy
1342 				 * mode.
1343 				 */
1344 				ret = 0;
1345 			else
1346 				vcdev->revision--;
1347 		}
1348 	} while (ret == -EOPNOTSUPP);
1349 
1350 	ccw_device_dma_free(vcdev->cdev, ccw, sizeof(*ccw));
1351 	ccw_device_dma_free(vcdev->cdev, rev, sizeof(*rev));
1352 	return ret;
1353 }
1354 
virtio_ccw_online(struct ccw_device * cdev)1355 static int virtio_ccw_online(struct ccw_device *cdev)
1356 {
1357 	int ret;
1358 	struct virtio_ccw_device *vcdev;
1359 	unsigned long flags;
1360 
1361 	vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1362 	if (!vcdev) {
1363 		dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1364 		ret = -ENOMEM;
1365 		goto out_free;
1366 	}
1367 	vcdev->vdev.dev.parent = &cdev->dev;
1368 	vcdev->cdev = cdev;
1369 	vcdev->dma_area = ccw_device_dma_zalloc(vcdev->cdev,
1370 						sizeof(*vcdev->dma_area),
1371 						&vcdev->dma_area_addr);
1372 	if (!vcdev->dma_area) {
1373 		ret = -ENOMEM;
1374 		goto out_free;
1375 	}
1376 
1377 	vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1378 
1379 	vcdev->vdev.dev.release = virtio_ccw_release_dev;
1380 	vcdev->vdev.config = &virtio_ccw_config_ops;
1381 	init_waitqueue_head(&vcdev->wait_q);
1382 	INIT_LIST_HEAD(&vcdev->virtqueues);
1383 	spin_lock_init(&vcdev->lock);
1384 	rwlock_init(&vcdev->irq_lock);
1385 	mutex_init(&vcdev->io_lock);
1386 
1387 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1388 	dev_set_drvdata(&cdev->dev, vcdev);
1389 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1390 	vcdev->vdev.id.vendor = cdev->id.cu_type;
1391 	vcdev->vdev.id.device = cdev->id.cu_model;
1392 
1393 	ret = virtio_ccw_set_transport_rev(vcdev);
1394 	if (ret)
1395 		goto out_free;
1396 
1397 	ret = register_virtio_device(&vcdev->vdev);
1398 	if (ret) {
1399 		dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1400 			 ret);
1401 		goto out_put;
1402 	}
1403 	return 0;
1404 out_put:
1405 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1406 	dev_set_drvdata(&cdev->dev, NULL);
1407 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1408 	put_device(&vcdev->vdev.dev);
1409 	return ret;
1410 out_free:
1411 	if (vcdev) {
1412 		ccw_device_dma_free(vcdev->cdev, vcdev->dma_area,
1413 				    sizeof(*vcdev->dma_area));
1414 	}
1415 	kfree(vcdev);
1416 	return ret;
1417 }
1418 
virtio_ccw_cio_notify(struct ccw_device * cdev,int event)1419 static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1420 {
1421 	int rc;
1422 	struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1423 
1424 	/*
1425 	 * Make sure vcdev is set
1426 	 * i.e. set_offline/remove callback not already running
1427 	 */
1428 	if (!vcdev)
1429 		return NOTIFY_DONE;
1430 
1431 	switch (event) {
1432 	case CIO_GONE:
1433 		vcdev->device_lost = true;
1434 		rc = NOTIFY_DONE;
1435 		break;
1436 	case CIO_OPER:
1437 		rc = NOTIFY_OK;
1438 		break;
1439 	default:
1440 		rc = NOTIFY_DONE;
1441 		break;
1442 	}
1443 	return rc;
1444 }
1445 
1446 static struct ccw_device_id virtio_ids[] = {
1447 	{ CCW_DEVICE(0x3832, 0) },
1448 	{},
1449 };
1450 
1451 static struct ccw_driver virtio_ccw_driver = {
1452 	.driver = {
1453 		.owner = THIS_MODULE,
1454 		.name = "virtio_ccw",
1455 	},
1456 	.ids = virtio_ids,
1457 	.probe = virtio_ccw_probe,
1458 	.remove = virtio_ccw_remove,
1459 	.set_offline = virtio_ccw_offline,
1460 	.set_online = virtio_ccw_online,
1461 	.notify = virtio_ccw_cio_notify,
1462 	.int_class = IRQIO_VIR,
1463 };
1464 
pure_hex(char ** cp,unsigned int * val,int min_digit,int max_digit,int max_val)1465 static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1466 			   int max_digit, int max_val)
1467 {
1468 	int diff;
1469 
1470 	diff = 0;
1471 	*val = 0;
1472 
1473 	while (diff <= max_digit) {
1474 		int value = hex_to_bin(**cp);
1475 
1476 		if (value < 0)
1477 			break;
1478 		*val = *val * 16 + value;
1479 		(*cp)++;
1480 		diff++;
1481 	}
1482 
1483 	if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1484 		return 1;
1485 
1486 	return 0;
1487 }
1488 
parse_busid(char * str,unsigned int * cssid,unsigned int * ssid,unsigned int * devno)1489 static int __init parse_busid(char *str, unsigned int *cssid,
1490 			      unsigned int *ssid, unsigned int *devno)
1491 {
1492 	char *str_work;
1493 	int rc, ret;
1494 
1495 	rc = 1;
1496 
1497 	if (*str == '\0')
1498 		goto out;
1499 
1500 	str_work = str;
1501 	ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1502 	if (ret || (str_work[0] != '.'))
1503 		goto out;
1504 	str_work++;
1505 	ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1506 	if (ret || (str_work[0] != '.'))
1507 		goto out;
1508 	str_work++;
1509 	ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1510 	if (ret || (str_work[0] != '\0'))
1511 		goto out;
1512 
1513 	rc = 0;
1514 out:
1515 	return rc;
1516 }
1517 
no_auto_parse(void)1518 static void __init no_auto_parse(void)
1519 {
1520 	unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1521 	char *parm, *str;
1522 	int rc;
1523 
1524 	str = no_auto;
1525 	while ((parm = strsep(&str, ","))) {
1526 		rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1527 				 &from_ssid, &from);
1528 		if (rc)
1529 			continue;
1530 		if (parm != NULL) {
1531 			rc = parse_busid(parm, &to_cssid,
1532 					 &to_ssid, &to);
1533 			if ((from_ssid > to_ssid) ||
1534 			    ((from_ssid == to_ssid) && (from > to)))
1535 				rc = -EINVAL;
1536 		} else {
1537 			to_cssid = from_cssid;
1538 			to_ssid = from_ssid;
1539 			to = from;
1540 		}
1541 		if (rc)
1542 			continue;
1543 		while ((from_ssid < to_ssid) ||
1544 		       ((from_ssid == to_ssid) && (from <= to))) {
1545 			set_bit(from, devs_no_auto[from_ssid]);
1546 			from++;
1547 			if (from > __MAX_SUBCHANNEL) {
1548 				from_ssid++;
1549 				from = 0;
1550 			}
1551 		}
1552 	}
1553 }
1554 
virtio_ccw_init(void)1555 static int __init virtio_ccw_init(void)
1556 {
1557 	int rc;
1558 
1559 	/* parse no_auto string before we do anything further */
1560 	no_auto_parse();
1561 
1562 	summary_indicators = cio_dma_zalloc(MAX_AIRQ_AREAS);
1563 	if (!summary_indicators)
1564 		return -ENOMEM;
1565 	rc = ccw_driver_register(&virtio_ccw_driver);
1566 	if (rc)
1567 		cio_dma_free(summary_indicators, MAX_AIRQ_AREAS);
1568 	return rc;
1569 }
1570 device_initcall(virtio_ccw_init);
1571