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