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