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