blob: ba2e0856d22cdfb5396457366276e01bc9ac7851 [file] [log] [blame]
Greg Kroah-Hartman31fb1672017-11-14 18:38:06 +01001// SPDX-License-Identifier: GPL-2.0
Cornelia Huck7e64e052012-12-14 17:02:18 +01002/*
3 * ccw based virtio transport
4 *
Cornelia Huck96b14532013-02-06 10:23:39 +01005 * Copyright IBM Corp. 2012, 2014
Cornelia Huck7e64e052012-12-14 17:02:18 +01006 *
Cornelia Huck7e64e052012-12-14 17:02:18 +01007 * 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/bootmem.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>
Paul Gortmakeracc50ec2016-10-30 16:37:32 -040024#include <linux/moduleparam.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010025#include <linux/io.h>
26#include <linux/kvm_para.h>
Heinz Graalfse75279c2014-04-28 11:24:05 +093027#include <linux/notifier.h>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020028#include <asm/diag.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010029#include <asm/setup.h>
30#include <asm/irq.h>
31#include <asm/cio.h>
32#include <asm/ccwdev.h>
Cornelia Huck6a773cb2013-02-28 12:33:17 +010033#include <asm/virtio-ccw.h>
Cornelia Huck96b14532013-02-06 10:23:39 +010034#include <asm/isc.h>
35#include <asm/airq.h>
Cornelia Huck7e64e052012-12-14 17:02:18 +010036
37/*
38 * virtio related functions
39 */
40
41struct 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
49struct virtio_ccw_device {
50 struct virtio_device vdev;
Cornelia Huck73fa21e2013-01-07 15:51:51 +010051 __u8 *status;
Cornelia Huck7e64e052012-12-14 17:02:18 +010052 __u8 config[VIRTIO_CCW_CONFIG_SIZE];
53 struct ccw_device *cdev;
Cornelia Huck7e64e052012-12-14 17:02:18 +010054 __u32 curr_io;
55 int err;
Thomas Huth6bb2c832014-10-07 16:39:50 +020056 unsigned int revision; /* Transport revision */
Cornelia Huck7e64e052012-12-14 17:02:18 +010057 wait_queue_head_t wait_q;
58 spinlock_t lock;
59 struct list_head virtqueues;
60 unsigned long indicators;
61 unsigned long indicators2;
62 struct vq_config_block *config_block;
Cornelia Huck96b14532013-02-06 10:23:39 +010063 bool is_thinint;
Heinz Graalfs79629b22014-03-05 15:23:54 +010064 bool going_away;
Heinz Graalfse75279c2014-04-28 11:24:05 +093065 bool device_lost;
Cornelia Huck431dae72015-06-29 16:44:01 +020066 unsigned int config_ready;
Cornelia Huck96b14532013-02-06 10:23:39 +010067 void *airq_info;
Cornelia Huck7e64e052012-12-14 17:02:18 +010068};
69
Cornelia Huckd4674242014-10-07 16:39:51 +020070struct vq_info_block_legacy {
Cornelia Huck7e64e052012-12-14 17:02:18 +010071 __u64 queue;
72 __u32 align;
73 __u16 index;
74 __u16 num;
75} __packed;
76
Cornelia Huckd4674242014-10-07 16:39:51 +020077struct vq_info_block {
78 __u64 desc;
79 __u32 res0;
80 __u16 index;
81 __u16 num;
82 __u64 avail;
83 __u64 used;
84} __packed;
85
Cornelia Huck7e64e052012-12-14 17:02:18 +010086struct virtio_feature_desc {
Heiko Carstensfb3170022017-05-09 12:50:53 +020087 __le32 features;
Cornelia Huck7e64e052012-12-14 17:02:18 +010088 __u8 index;
89} __packed;
90
Cornelia Huck96b14532013-02-06 10:23:39 +010091struct virtio_thinint_area {
92 unsigned long summary_indicator;
93 unsigned long indicator;
94 u64 bit_nr;
95 u8 isc;
96} __packed;
97
Thomas Huth6bb2c832014-10-07 16:39:50 +020098struct virtio_rev_info {
99 __u16 revision;
100 __u16 length;
101 __u8 data[];
102};
103
104/* the highest virtio-ccw revision we support */
Cornelia Huckaf9ca132014-10-07 16:39:52 +0200105#define VIRTIO_CCW_REV_MAX 1
Thomas Huth6bb2c832014-10-07 16:39:50 +0200106
Cornelia Huck7e64e052012-12-14 17:02:18 +0100107struct virtio_ccw_vq_info {
108 struct virtqueue *vq;
109 int num;
110 void *queue;
Cornelia Huckd4674242014-10-07 16:39:51 +0200111 union {
112 struct vq_info_block s;
113 struct vq_info_block_legacy l;
114 } *info_block;
Cornelia Huck96b14532013-02-06 10:23:39 +0100115 int bit_nr;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100116 struct list_head node;
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100117 long cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100118};
119
Cornelia Huck96b14532013-02-06 10:23:39 +0100120#define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
121
122#define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
123#define MAX_AIRQ_AREAS 20
124
125static int virtio_ccw_use_airq = 1;
126
127struct airq_info {
128 rwlock_t lock;
129 u8 summary_indicator;
130 struct airq_struct airq;
131 struct airq_iv *aiv;
132};
133static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
134
Cornelia Huck7e64e052012-12-14 17:02:18 +0100135#define CCW_CMD_SET_VQ 0x13
136#define CCW_CMD_VDEV_RESET 0x33
137#define CCW_CMD_SET_IND 0x43
138#define CCW_CMD_SET_CONF_IND 0x53
139#define CCW_CMD_READ_FEAT 0x12
140#define CCW_CMD_WRITE_FEAT 0x11
141#define CCW_CMD_READ_CONF 0x22
142#define CCW_CMD_WRITE_CONF 0x21
143#define CCW_CMD_WRITE_STATUS 0x31
144#define CCW_CMD_READ_VQ_CONF 0x32
Pierre Morel7d3ce5a2015-08-28 11:09:32 +0200145#define CCW_CMD_READ_STATUS 0x72
Cornelia Huck96b14532013-02-06 10:23:39 +0100146#define CCW_CMD_SET_IND_ADAPTER 0x73
Thomas Huth6bb2c832014-10-07 16:39:50 +0200147#define CCW_CMD_SET_VIRTIO_REV 0x83
Cornelia Huck7e64e052012-12-14 17:02:18 +0100148
149#define VIRTIO_CCW_DOING_SET_VQ 0x00010000
150#define VIRTIO_CCW_DOING_RESET 0x00040000
151#define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
152#define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
153#define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
154#define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
155#define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
156#define VIRTIO_CCW_DOING_SET_IND 0x01000000
157#define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
158#define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
Cornelia Huck96b14532013-02-06 10:23:39 +0100159#define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
Thomas Huth6bb2c832014-10-07 16:39:50 +0200160#define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
Pierre Morel7d3ce5a2015-08-28 11:09:32 +0200161#define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
Cornelia Huck7e64e052012-12-14 17:02:18 +0100162#define VIRTIO_CCW_INTPARM_MASK 0xffff0000
163
164static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
165{
166 return container_of(vdev, struct virtio_ccw_device, vdev);
167}
168
Cornelia Huck96b14532013-02-06 10:23:39 +0100169static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
170{
171 unsigned long i, flags;
172
173 write_lock_irqsave(&info->lock, flags);
174 for (i = 0; i < airq_iv_end(info->aiv); i++) {
175 if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
176 airq_iv_free_bit(info->aiv, i);
177 airq_iv_set_ptr(info->aiv, i, 0);
178 break;
179 }
180 }
181 write_unlock_irqrestore(&info->lock, flags);
182}
183
184static void virtio_airq_handler(struct airq_struct *airq)
185{
186 struct airq_info *info = container_of(airq, struct airq_info, airq);
187 unsigned long ai;
188
189 inc_irq_stat(IRQIO_VAI);
190 read_lock(&info->lock);
191 /* Walk through indicators field, summary indicator active. */
192 for (ai = 0;;) {
193 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
194 if (ai == -1UL)
195 break;
196 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
197 }
198 info->summary_indicator = 0;
199 smp_wmb();
200 /* Walk through indicators field, summary indicator not active. */
201 for (ai = 0;;) {
202 ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
203 if (ai == -1UL)
204 break;
205 vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
206 }
207 read_unlock(&info->lock);
208}
209
210static struct airq_info *new_airq_info(void)
211{
212 struct airq_info *info;
213 int rc;
214
215 info = kzalloc(sizeof(*info), GFP_KERNEL);
216 if (!info)
217 return NULL;
218 rwlock_init(&info->lock);
219 info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
220 if (!info->aiv) {
221 kfree(info);
222 return NULL;
223 }
224 info->airq.handler = virtio_airq_handler;
225 info->airq.lsi_ptr = &info->summary_indicator;
226 info->airq.lsi_mask = 0xff;
227 info->airq.isc = VIRTIO_AIRQ_ISC;
228 rc = register_adapter_interrupt(&info->airq);
229 if (rc) {
230 airq_iv_release(info->aiv);
231 kfree(info);
232 return NULL;
233 }
234 return info;
235}
236
Cornelia Huck96b14532013-02-06 10:23:39 +0100237static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
238 u64 *first, void **airq_info)
239{
240 int i, j;
241 struct airq_info *info;
242 unsigned long indicator_addr = 0;
243 unsigned long bit, flags;
244
245 for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
246 if (!airq_areas[i])
247 airq_areas[i] = new_airq_info();
248 info = airq_areas[i];
249 if (!info)
250 return 0;
251 write_lock_irqsave(&info->lock, flags);
252 bit = airq_iv_alloc(info->aiv, nvqs);
253 if (bit == -1UL) {
254 /* Not enough vacancies. */
255 write_unlock_irqrestore(&info->lock, flags);
256 continue;
257 }
258 *first = bit;
259 *airq_info = info;
260 indicator_addr = (unsigned long)info->aiv->vector;
261 for (j = 0; j < nvqs; j++) {
262 airq_iv_set_ptr(info->aiv, bit + j,
263 (unsigned long)vqs[j]);
264 }
265 write_unlock_irqrestore(&info->lock, flags);
266 }
267 return indicator_addr;
268}
269
270static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
271{
272 struct virtio_ccw_vq_info *info;
273
274 list_for_each_entry(info, &vcdev->virtqueues, node)
275 drop_airq_indicator(info->vq, vcdev->airq_info);
276}
277
Cornelia Huck7e64e052012-12-14 17:02:18 +0100278static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
279{
280 unsigned long flags;
281 __u32 ret;
282
283 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
284 if (vcdev->err)
285 ret = 0;
286 else
287 ret = vcdev->curr_io & flag;
288 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
289 return ret;
290}
291
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100292static int ccw_io_helper(struct virtio_ccw_device *vcdev,
293 struct ccw1 *ccw, __u32 intparm)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100294{
295 int ret;
296 unsigned long flags;
297 int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
298
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100299 do {
300 spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
301 ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
Cornelia Huck99437a22013-04-04 10:25:06 +0200302 if (!ret) {
303 if (!vcdev->curr_io)
304 vcdev->err = 0;
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100305 vcdev->curr_io |= flag;
Cornelia Huck99437a22013-04-04 10:25:06 +0200306 }
Christian Borntraegerb26ba222013-01-07 15:51:52 +0100307 spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
308 cpu_relax();
309 } while (ret == -EBUSY);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100310 wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
311 return ret ? ret : vcdev->err;
312}
313
Cornelia Huck96b14532013-02-06 10:23:39 +0100314static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
315 struct ccw1 *ccw)
316{
317 int ret;
318 unsigned long *indicatorp = NULL;
319 struct virtio_thinint_area *thinint_area = NULL;
320 struct airq_info *airq_info = vcdev->airq_info;
321
322 if (vcdev->is_thinint) {
323 thinint_area = kzalloc(sizeof(*thinint_area),
324 GFP_DMA | GFP_KERNEL);
325 if (!thinint_area)
326 return;
327 thinint_area->summary_indicator =
328 (unsigned long) &airq_info->summary_indicator;
329 thinint_area->isc = VIRTIO_AIRQ_ISC;
330 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
331 ccw->count = sizeof(*thinint_area);
332 ccw->cda = (__u32)(unsigned long) thinint_area;
333 } else {
Cornelia Huckc2350822016-03-01 13:44:53 +0100334 /* payload is the address of the indicators */
Cornelia Huck96b14532013-02-06 10:23:39 +0100335 indicatorp = kmalloc(sizeof(&vcdev->indicators),
336 GFP_DMA | GFP_KERNEL);
337 if (!indicatorp)
338 return;
339 *indicatorp = 0;
340 ccw->cmd_code = CCW_CMD_SET_IND;
Cornelia Huckc2350822016-03-01 13:44:53 +0100341 ccw->count = sizeof(&vcdev->indicators);
Cornelia Huck96b14532013-02-06 10:23:39 +0100342 ccw->cda = (__u32)(unsigned long) indicatorp;
343 }
344 /* Deregister indicators from host. */
345 vcdev->indicators = 0;
346 ccw->flags = 0;
347 ret = ccw_io_helper(vcdev, ccw,
348 vcdev->is_thinint ?
349 VIRTIO_CCW_DOING_SET_IND_ADAPTER :
350 VIRTIO_CCW_DOING_SET_IND);
351 if (ret && (ret != -ENODEV))
352 dev_info(&vcdev->cdev->dev,
353 "Failed to deregister indicators (%d)\n", ret);
354 else if (vcdev->is_thinint)
355 virtio_ccw_drop_indicators(vcdev);
356 kfree(indicatorp);
357 kfree(thinint_area);
358}
359
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200360static inline long __do_kvm_notify(struct subchannel_id schid,
361 unsigned long queue_index,
362 long cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100363{
364 register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
365 register struct subchannel_id __schid asm("2") = schid;
366 register unsigned long __index asm("3") = queue_index;
367 register long __rc asm("2");
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100368 register long __cookie asm("4") = cookie;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100369
370 asm volatile ("diag 2,4,0x500\n"
Michael S. Tsirkin07e16932013-02-28 12:33:16 +0100371 : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
372 "d"(__cookie)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100373 : "memory", "cc");
374 return __rc;
375}
376
Martin Schwidefsky1ec27722015-08-20 17:28:44 +0200377static inline long do_kvm_notify(struct subchannel_id schid,
378 unsigned long queue_index,
379 long cookie)
380{
381 diag_stat_inc(DIAG_STAT_X500);
382 return __do_kvm_notify(schid, queue_index, cookie);
383}
384
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030385static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100386{
387 struct virtio_ccw_vq_info *info = vq->priv;
388 struct virtio_ccw_device *vcdev;
389 struct subchannel_id schid;
390
391 vcdev = to_vc_device(info->vq->vdev);
392 ccw_device_get_schid(vcdev->cdev, &schid);
Linus Torvalds01227a82013-05-05 14:47:31 -0700393 info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030394 if (info->cookie < 0)
395 return false;
396 return true;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100397}
398
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100399static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
400 struct ccw1 *ccw, int index)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100401{
Pierre Morelad2aa042015-09-10 16:35:08 +0200402 int ret;
403
Cornelia Huck7e64e052012-12-14 17:02:18 +0100404 vcdev->config_block->index = index;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100405 ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
406 ccw->flags = 0;
407 ccw->count = sizeof(struct vq_config_block);
408 ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
Pierre Morelad2aa042015-09-10 16:35:08 +0200409 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
410 if (ret)
411 return ret;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100412 return vcdev->config_block->num;
413}
414
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100415static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100416{
417 struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
418 struct virtio_ccw_vq_info *info = vq->priv;
419 unsigned long flags;
420 unsigned long size;
421 int ret;
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000422 unsigned int index = vq->index;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100423
424 /* Remove from our list. */
425 spin_lock_irqsave(&vcdev->lock, flags);
426 list_del(&info->node);
427 spin_unlock_irqrestore(&vcdev->lock, flags);
428
429 /* Release from host. */
Cornelia Huckd4674242014-10-07 16:39:51 +0200430 if (vcdev->revision == 0) {
431 info->info_block->l.queue = 0;
432 info->info_block->l.align = 0;
433 info->info_block->l.index = index;
434 info->info_block->l.num = 0;
435 ccw->count = sizeof(info->info_block->l);
436 } else {
437 info->info_block->s.desc = 0;
438 info->info_block->s.index = index;
439 info->info_block->s.num = 0;
440 info->info_block->s.avail = 0;
441 info->info_block->s.used = 0;
442 ccw->count = sizeof(info->info_block->s);
443 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100444 ccw->cmd_code = CCW_CMD_SET_VQ;
445 ccw->flags = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100446 ccw->cda = (__u32)(unsigned long)(info->info_block);
447 ret = ccw_io_helper(vcdev, ccw,
448 VIRTIO_CCW_DOING_SET_VQ | index);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100449 /*
450 * -ENODEV isn't considered an error: The device is gone anyway.
451 * This may happen on device detach.
452 */
453 if (ret && (ret != -ENODEV))
Colin Ian King99240622016-09-27 13:08:44 -0700454 dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
Cornelia Huck7e64e052012-12-14 17:02:18 +0100455 ret, index);
456
457 vring_del_virtqueue(vq);
458 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
459 free_pages_exact(info->queue, size);
460 kfree(info->info_block);
461 kfree(info);
462}
463
464static void virtio_ccw_del_vqs(struct virtio_device *vdev)
465{
466 struct virtqueue *vq, *n;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100467 struct ccw1 *ccw;
Cornelia Huck96b14532013-02-06 10:23:39 +0100468 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100469
470 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
471 if (!ccw)
472 return;
473
Cornelia Huck96b14532013-02-06 10:23:39 +0100474 virtio_ccw_drop_indicator(vcdev, ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100475
476 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100477 virtio_ccw_del_vq(vq, ccw);
478
479 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100480}
481
482static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
483 int i, vq_callback_t *callback,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200484 const char *name, bool ctx,
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100485 struct ccw1 *ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100486{
487 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
488 int err;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100489 struct virtqueue *vq = NULL;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100490 struct virtio_ccw_vq_info *info;
Cornelia Huckc98d3682013-01-25 15:34:16 +0100491 unsigned long size = 0; /* silence the compiler */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100492 unsigned long flags;
493
494 /* Allocate queue. */
495 info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
496 if (!info) {
497 dev_warn(&vcdev->cdev->dev, "no info\n");
498 err = -ENOMEM;
499 goto out_err;
500 }
501 info->info_block = kzalloc(sizeof(*info->info_block),
502 GFP_DMA | GFP_KERNEL);
503 if (!info->info_block) {
504 dev_warn(&vcdev->cdev->dev, "no info block\n");
505 err = -ENOMEM;
506 goto out_err;
507 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100508 info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
Pierre Morelad2aa042015-09-10 16:35:08 +0200509 if (info->num < 0) {
510 err = info->num;
511 goto out_err;
512 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100513 size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
514 info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
515 if (info->queue == NULL) {
516 dev_warn(&vcdev->cdev->dev, "no queue\n");
517 err = -ENOMEM;
518 goto out_err;
519 }
520
521 vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200522 true, ctx, info->queue, virtio_ccw_kvm_notify,
Cornelia Huck7e64e052012-12-14 17:02:18 +0100523 callback, name);
524 if (!vq) {
525 /* For now, we fail if we can't get the requested size. */
526 dev_warn(&vcdev->cdev->dev, "no vq\n");
527 err = -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100528 goto out_err;
529 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100530
531 /* Register it with the host. */
Cornelia Huckd4674242014-10-07 16:39:51 +0200532 if (vcdev->revision == 0) {
533 info->info_block->l.queue = (__u64)info->queue;
534 info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
535 info->info_block->l.index = i;
536 info->info_block->l.num = info->num;
537 ccw->count = sizeof(info->info_block->l);
538 } else {
539 info->info_block->s.desc = (__u64)info->queue;
540 info->info_block->s.index = i;
541 info->info_block->s.num = info->num;
542 info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
543 info->info_block->s.used = (__u64)virtqueue_get_used(vq);
544 ccw->count = sizeof(info->info_block->s);
545 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100546 ccw->cmd_code = CCW_CMD_SET_VQ;
547 ccw->flags = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100548 ccw->cda = (__u32)(unsigned long)(info->info_block);
549 err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100550 if (err) {
551 dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
Cornelia Huck7e64e052012-12-14 17:02:18 +0100552 goto out_err;
553 }
554
Cornelia Huckc98d3682013-01-25 15:34:16 +0100555 info->vq = vq;
556 vq->priv = info;
557
Cornelia Huck7e64e052012-12-14 17:02:18 +0100558 /* Save it to our list. */
559 spin_lock_irqsave(&vcdev->lock, flags);
560 list_add(&info->node, &vcdev->virtqueues);
561 spin_unlock_irqrestore(&vcdev->lock, flags);
562
563 return vq;
564
565out_err:
Cornelia Huckc98d3682013-01-25 15:34:16 +0100566 if (vq)
567 vring_del_virtqueue(vq);
568 if (info) {
569 if (info->queue)
570 free_pages_exact(info->queue, size);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100571 kfree(info->info_block);
Cornelia Huckc98d3682013-01-25 15:34:16 +0100572 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100573 kfree(info);
574 return ERR_PTR(err);
575}
576
Cornelia Huck96b14532013-02-06 10:23:39 +0100577static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
578 struct virtqueue *vqs[], int nvqs,
579 struct ccw1 *ccw)
580{
581 int ret;
582 struct virtio_thinint_area *thinint_area = NULL;
583 struct airq_info *info;
584
585 thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
586 if (!thinint_area) {
587 ret = -ENOMEM;
588 goto out;
589 }
590 /* Try to get an indicator. */
591 thinint_area->indicator = get_airq_indicator(vqs, nvqs,
592 &thinint_area->bit_nr,
593 &vcdev->airq_info);
594 if (!thinint_area->indicator) {
595 ret = -ENOSPC;
596 goto out;
597 }
598 info = vcdev->airq_info;
599 thinint_area->summary_indicator =
600 (unsigned long) &info->summary_indicator;
601 thinint_area->isc = VIRTIO_AIRQ_ISC;
602 ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
603 ccw->flags = CCW_FLAG_SLI;
604 ccw->count = sizeof(*thinint_area);
605 ccw->cda = (__u32)(unsigned long)thinint_area;
606 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
607 if (ret) {
608 if (ret == -EOPNOTSUPP) {
609 /*
610 * The host does not support adapter interrupts
611 * for virtio-ccw, stop trying.
612 */
613 virtio_ccw_use_airq = 0;
614 pr_info("Adapter interrupts unsupported on host\n");
615 } else
616 dev_warn(&vcdev->cdev->dev,
617 "enabling adapter interrupts = %d\n", ret);
618 virtio_ccw_drop_indicators(vcdev);
619 }
620out:
621 kfree(thinint_area);
622 return ret;
623}
624
Cornelia Huck7e64e052012-12-14 17:02:18 +0100625static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
626 struct virtqueue *vqs[],
627 vq_callback_t *callbacks[],
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100628 const char * const names[],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200629 const bool *ctx,
Christoph Hellwigfb5e31d2017-02-05 18:15:22 +0100630 struct irq_affinity *desc)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100631{
632 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
633 unsigned long *indicatorp = NULL;
634 int ret, i;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100635 struct ccw1 *ccw;
636
637 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
638 if (!ccw)
639 return -ENOMEM;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100640
641 for (i = 0; i < nvqs; ++i) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100642 vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
Michael S. Tsirkinf94682d2017-03-06 18:32:29 +0200643 ctx ? ctx[i] : false, ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100644 if (IS_ERR(vqs[i])) {
645 ret = PTR_ERR(vqs[i]);
646 vqs[i] = NULL;
647 goto out;
648 }
649 }
650 ret = -ENOMEM;
Cornelia Huckc2350822016-03-01 13:44:53 +0100651 /*
652 * We need a data area under 2G to communicate. Our payload is
653 * the address of the indicators.
654 */
Cornelia Huck7e64e052012-12-14 17:02:18 +0100655 indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
656 if (!indicatorp)
657 goto out;
658 *indicatorp = (unsigned long) &vcdev->indicators;
Cornelia Huck96b14532013-02-06 10:23:39 +0100659 if (vcdev->is_thinint) {
660 ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
661 if (ret)
662 /* no error, just fall back to legacy interrupts */
Heiko Carstens970ba6ac2017-01-02 09:59:40 +0100663 vcdev->is_thinint = false;
Cornelia Huck96b14532013-02-06 10:23:39 +0100664 }
665 if (!vcdev->is_thinint) {
666 /* Register queue indicators with host. */
667 vcdev->indicators = 0;
668 ccw->cmd_code = CCW_CMD_SET_IND;
669 ccw->flags = 0;
Cornelia Huckc2350822016-03-01 13:44:53 +0100670 ccw->count = sizeof(&vcdev->indicators);
Cornelia Huck96b14532013-02-06 10:23:39 +0100671 ccw->cda = (__u32)(unsigned long) indicatorp;
672 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
673 if (ret)
674 goto out;
675 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100676 /* Register indicators2 with host for config changes */
677 *indicatorp = (unsigned long) &vcdev->indicators2;
678 vcdev->indicators2 = 0;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100679 ccw->cmd_code = CCW_CMD_SET_CONF_IND;
680 ccw->flags = 0;
Cornelia Huckc2350822016-03-01 13:44:53 +0100681 ccw->count = sizeof(&vcdev->indicators2);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100682 ccw->cda = (__u32)(unsigned long) indicatorp;
683 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100684 if (ret)
685 goto out;
686
687 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100688 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100689 return 0;
690out:
691 kfree(indicatorp);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100692 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100693 virtio_ccw_del_vqs(vdev);
694 return ret;
695}
696
697static void virtio_ccw_reset(struct virtio_device *vdev)
698{
699 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100700 struct ccw1 *ccw;
701
702 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
703 if (!ccw)
704 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100705
706 /* Zero status bits. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100707 *vcdev->status = 0;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100708
709 /* Send a reset ccw on device. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100710 ccw->cmd_code = CCW_CMD_VDEV_RESET;
711 ccw->flags = 0;
712 ccw->count = 0;
713 ccw->cda = 0;
714 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
715 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100716}
717
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200718static u64 virtio_ccw_get_features(struct virtio_device *vdev)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100719{
720 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100721 struct virtio_feature_desc *features;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200722 int ret;
723 u64 rc;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100724 struct ccw1 *ccw;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100725
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100726 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
727 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100728 return 0;
729
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100730 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
731 if (!features) {
732 rc = 0;
733 goto out_free;
734 }
735 /* Read the feature bits from the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100736 features->index = 0;
737 ccw->cmd_code = CCW_CMD_READ_FEAT;
738 ccw->flags = 0;
739 ccw->count = sizeof(*features);
740 ccw->cda = (__u32)(unsigned long)features;
741 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
742 if (ret) {
743 rc = 0;
744 goto out_free;
745 }
746
747 rc = le32_to_cpu(features->features);
748
Michael S. Tsirkince154082014-12-04 18:59:50 +0200749 if (vcdev->revision == 0)
750 goto out_free;
751
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200752 /* Read second half of the feature bits from the host. */
753 features->index = 1;
754 ccw->cmd_code = CCW_CMD_READ_FEAT;
755 ccw->flags = 0;
756 ccw->count = sizeof(*features);
757 ccw->cda = (__u32)(unsigned long)features;
758 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
759 if (ret == 0)
760 rc |= (u64)le32_to_cpu(features->features) << 32;
761
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100762out_free:
763 kfree(features);
764 kfree(ccw);
765 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100766}
767
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200768static int virtio_ccw_finalize_features(struct virtio_device *vdev)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100769{
770 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100771 struct virtio_feature_desc *features;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100772 struct ccw1 *ccw;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100773 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100774
Michael S. Tsirkinf13d8bc2014-12-09 14:46:44 +0200775 if (vcdev->revision >= 1 &&
Michael S. Tsirkin4d236762014-12-04 19:16:43 +0200776 !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
777 dev_err(&vdev->dev, "virtio: device uses revision 1 "
778 "but does not have VIRTIO_F_VERSION_1\n");
779 return -EINVAL;
780 }
781
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100782 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
783 if (!ccw)
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100784 return -ENOMEM;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100785
786 features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100787 if (!features) {
788 ret = -ENOMEM;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100789 goto out_free;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100790 }
Cornelia Huck7e64e052012-12-14 17:02:18 +0100791 /* Give virtio_ring a chance to accept features. */
792 vring_transport_features(vdev);
793
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200794 features->index = 0;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200795 features->features = cpu_to_le32((u32)vdev->features);
796 /* Write the first half of the feature bits to the host. */
797 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
798 ccw->flags = 0;
799 ccw->count = sizeof(*features);
800 ccw->cda = (__u32)(unsigned long)features;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100801 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
802 if (ret)
803 goto out_free;
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200804
Michael S. Tsirkince154082014-12-04 18:59:50 +0200805 if (vcdev->revision == 0)
806 goto out_free;
807
Michael S. Tsirkin732c56e2014-11-27 13:54:28 +0200808 features->index = 1;
809 features->features = cpu_to_le32(vdev->features >> 32);
810 /* Write the second half of the feature bits to the host. */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200811 ccw->cmd_code = CCW_CMD_WRITE_FEAT;
812 ccw->flags = 0;
813 ccw->count = sizeof(*features);
814 ccw->cda = (__u32)(unsigned long)features;
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100815 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200816
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100817out_free:
818 kfree(features);
819 kfree(ccw);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200820
Cornelia Huckf01a2a82014-12-09 11:46:59 +0100821 return ret;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100822}
823
824static void virtio_ccw_get_config(struct virtio_device *vdev,
825 unsigned int offset, void *buf, unsigned len)
826{
827 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
828 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100829 struct ccw1 *ccw;
830 void *config_area;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100831
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100832 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
833 if (!ccw)
Cornelia Huck7e64e052012-12-14 17:02:18 +0100834 return;
835
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100836 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
837 if (!config_area)
838 goto out_free;
839
840 /* Read the config area from the host. */
841 ccw->cmd_code = CCW_CMD_READ_CONF;
842 ccw->flags = 0;
843 ccw->count = offset + len;
844 ccw->cda = (__u32)(unsigned long)config_area;
845 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
846 if (ret)
847 goto out_free;
848
Cornelia Huck431dae72015-06-29 16:44:01 +0200849 memcpy(vcdev->config, config_area, offset + len);
850 if (buf)
851 memcpy(buf, &vcdev->config[offset], len);
852 if (vcdev->config_ready < offset + len)
853 vcdev->config_ready = offset + len;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100854
855out_free:
856 kfree(config_area);
857 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100858}
859
860static void virtio_ccw_set_config(struct virtio_device *vdev,
861 unsigned int offset, const void *buf,
862 unsigned len)
863{
864 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100865 struct ccw1 *ccw;
866 void *config_area;
867
868 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
869 if (!ccw)
870 return;
871
872 config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
873 if (!config_area)
874 goto out_free;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100875
Cornelia Huck431dae72015-06-29 16:44:01 +0200876 /* Make sure we don't overwrite fields. */
877 if (vcdev->config_ready < offset)
878 virtio_ccw_get_config(vdev, 0, NULL, offset);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100879 memcpy(&vcdev->config[offset], buf, len);
880 /* Write the config area to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100881 memcpy(config_area, vcdev->config, sizeof(vcdev->config));
882 ccw->cmd_code = CCW_CMD_WRITE_CONF;
883 ccw->flags = 0;
884 ccw->count = offset + len;
885 ccw->cda = (__u32)(unsigned long)config_area;
886 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
887
888out_free:
889 kfree(config_area);
890 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100891}
892
893static u8 virtio_ccw_get_status(struct virtio_device *vdev)
894{
895 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Pierre Morel7d3ce5a2015-08-28 11:09:32 +0200896 u8 old_status = *vcdev->status;
897 struct ccw1 *ccw;
898
899 if (vcdev->revision < 1)
900 return *vcdev->status;
901
902 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
903 if (!ccw)
904 return old_status;
905
906 ccw->cmd_code = CCW_CMD_READ_STATUS;
907 ccw->flags = 0;
908 ccw->count = sizeof(*vcdev->status);
909 ccw->cda = (__u32)(unsigned long)vcdev->status;
910 ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
911/*
912 * If the channel program failed (should only happen if the device
913 * was hotunplugged, and then we clean up via the machine check
914 * handler anyway), vcdev->status was not overwritten and we just
915 * return the old status, which is fine.
916*/
917 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100918
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100919 return *vcdev->status;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100920}
921
922static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
923{
924 struct virtio_ccw_device *vcdev = to_vc_device(vdev);
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300925 u8 old_status = *vcdev->status;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100926 struct ccw1 *ccw;
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300927 int ret;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100928
929 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
930 if (!ccw)
931 return;
Cornelia Huck7e64e052012-12-14 17:02:18 +0100932
933 /* Write the status to the host. */
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100934 *vcdev->status = status;
935 ccw->cmd_code = CCW_CMD_WRITE_STATUS;
936 ccw->flags = 0;
937 ccw->count = sizeof(status);
938 ccw->cda = (__u32)(unsigned long)vcdev->status;
Michael S. Tsirkin48555132014-10-23 18:40:30 +0300939 ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
940 /* Write failed? We assume status is unchanged. */
941 if (ret)
942 *vcdev->status = old_status;
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100943 kfree(ccw);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100944}
945
Bhumika Goyal0db1dba2017-01-14 00:18:56 +0530946static const struct virtio_config_ops virtio_ccw_config_ops = {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100947 .get_features = virtio_ccw_get_features,
948 .finalize_features = virtio_ccw_finalize_features,
949 .get = virtio_ccw_get_config,
950 .set = virtio_ccw_set_config,
951 .get_status = virtio_ccw_get_status,
952 .set_status = virtio_ccw_set_status,
953 .reset = virtio_ccw_reset,
954 .find_vqs = virtio_ccw_find_vqs,
955 .del_vqs = virtio_ccw_del_vqs,
956};
957
958
959/*
960 * ccw bus driver related functions
961 */
962
963static void virtio_ccw_release_dev(struct device *_d)
964{
Geliang Tang15e90f52016-03-01 13:44:52 +0100965 struct virtio_device *dev = dev_to_virtio(_d);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100966 struct virtio_ccw_device *vcdev = to_vc_device(dev);
967
Cornelia Huck73fa21e2013-01-07 15:51:51 +0100968 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100969 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +0100970 kfree(vcdev);
971}
972
973static int irb_is_error(struct irb *irb)
974{
975 if (scsw_cstat(&irb->scsw) != 0)
976 return 1;
977 if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
978 return 1;
979 if (scsw_cc(&irb->scsw) != 0)
980 return 1;
981 return 0;
982}
983
984static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
985 int index)
986{
987 struct virtio_ccw_vq_info *info;
988 unsigned long flags;
989 struct virtqueue *vq;
990
991 vq = NULL;
992 spin_lock_irqsave(&vcdev->lock, flags);
993 list_for_each_entry(info, &vcdev->virtqueues, node) {
Rusty Russell9d0ca6e2013-03-21 14:17:34 +0000994 if (info->vq->index == index) {
Cornelia Huck7e64e052012-12-14 17:02:18 +0100995 vq = info->vq;
996 break;
997 }
998 }
999 spin_unlock_irqrestore(&vcdev->lock, flags);
1000 return vq;
1001}
1002
Cornelia Huck74a599f2015-12-03 17:24:00 +01001003static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
1004 __u32 activity)
Cornelia Huck7e64e052012-12-14 17:02:18 +01001005{
Cornelia Huck7e64e052012-12-14 17:02:18 +01001006 if (vcdev->curr_io & activity) {
1007 switch (activity) {
1008 case VIRTIO_CCW_DOING_READ_FEAT:
1009 case VIRTIO_CCW_DOING_WRITE_FEAT:
1010 case VIRTIO_CCW_DOING_READ_CONFIG:
1011 case VIRTIO_CCW_DOING_WRITE_CONFIG:
1012 case VIRTIO_CCW_DOING_WRITE_STATUS:
Pierre Morel7d3ce5a2015-08-28 11:09:32 +02001013 case VIRTIO_CCW_DOING_READ_STATUS:
Cornelia Huck7e64e052012-12-14 17:02:18 +01001014 case VIRTIO_CCW_DOING_SET_VQ:
1015 case VIRTIO_CCW_DOING_SET_IND:
1016 case VIRTIO_CCW_DOING_SET_CONF_IND:
1017 case VIRTIO_CCW_DOING_RESET:
1018 case VIRTIO_CCW_DOING_READ_VQ_CONF:
Cornelia Huck96b14532013-02-06 10:23:39 +01001019 case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
Thomas Huth6bb2c832014-10-07 16:39:50 +02001020 case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
Cornelia Huck7e64e052012-12-14 17:02:18 +01001021 vcdev->curr_io &= ~activity;
1022 wake_up(&vcdev->wait_q);
1023 break;
1024 default:
1025 /* don't know what to do... */
Cornelia Huck74a599f2015-12-03 17:24:00 +01001026 dev_warn(&vcdev->cdev->dev,
1027 "Suspicious activity '%08x'\n", activity);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001028 WARN_ON(1);
1029 break;
1030 }
1031 }
Cornelia Huck74a599f2015-12-03 17:24:00 +01001032}
1033
1034static void virtio_ccw_int_handler(struct ccw_device *cdev,
1035 unsigned long intparm,
1036 struct irb *irb)
1037{
1038 __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
1039 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1040 int i;
1041 struct virtqueue *vq;
1042
1043 if (!vcdev)
1044 return;
1045 if (IS_ERR(irb)) {
1046 vcdev->err = PTR_ERR(irb);
1047 virtio_ccw_check_activity(vcdev, activity);
1048 /* Don't poke around indicators, something's wrong. */
1049 return;
1050 }
1051 /* Check if it's a notification from the host. */
1052 if ((intparm == 0) &&
1053 (scsw_stctl(&irb->scsw) ==
1054 (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
1055 /* OK */
1056 }
1057 if (irb_is_error(irb)) {
1058 /* Command reject? */
1059 if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
1060 (irb->ecw[0] & SNS0_CMD_REJECT))
1061 vcdev->err = -EOPNOTSUPP;
1062 else
1063 /* Map everything else to -EIO. */
1064 vcdev->err = -EIO;
1065 }
1066 virtio_ccw_check_activity(vcdev, activity);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001067 for_each_set_bit(i, &vcdev->indicators,
1068 sizeof(vcdev->indicators) * BITS_PER_BYTE) {
1069 /* The bit clear must happen before the vring kick. */
1070 clear_bit(i, &vcdev->indicators);
1071 barrier();
1072 vq = virtio_ccw_vq_by_ind(vcdev, i);
1073 vring_interrupt(0, vq);
1074 }
1075 if (test_bit(0, &vcdev->indicators2)) {
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +10301076 virtio_config_changed(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001077 clear_bit(0, &vcdev->indicators2);
1078 }
1079}
1080
1081/*
1082 * We usually want to autoonline all devices, but give the admin
1083 * a way to exempt devices from this.
1084 */
1085#define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
1086 (8*sizeof(long)))
1087static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
1088
1089static char *no_auto = "";
1090
1091module_param(no_auto, charp, 0444);
1092MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
1093
1094static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
1095{
1096 struct ccw_dev_id id;
1097
1098 ccw_device_get_id(cdev, &id);
1099 if (test_bit(id.devno, devs_no_auto[id.ssid]))
1100 return 0;
1101 return 1;
1102}
1103
1104static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
1105{
1106 struct ccw_device *cdev = data;
1107 int ret;
1108
1109 ret = ccw_device_set_online(cdev);
1110 if (ret)
1111 dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
1112}
1113
1114static int virtio_ccw_probe(struct ccw_device *cdev)
1115{
1116 cdev->handler = virtio_ccw_int_handler;
1117
1118 if (virtio_ccw_check_autoonline(cdev))
1119 async_schedule(virtio_ccw_auto_online, cdev);
1120 return 0;
1121}
1122
Heinz Graalfs2e021042014-02-27 14:34:35 +01001123static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
1124{
1125 unsigned long flags;
1126 struct virtio_ccw_device *vcdev;
1127
1128 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1129 vcdev = dev_get_drvdata(&cdev->dev);
Heinz Graalfs79629b22014-03-05 15:23:54 +01001130 if (!vcdev || vcdev->going_away) {
Heinz Graalfs2e021042014-02-27 14:34:35 +01001131 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1132 return NULL;
1133 }
Heinz Graalfs79629b22014-03-05 15:23:54 +01001134 vcdev->going_away = true;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001135 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1136 return vcdev;
1137}
1138
Cornelia Huck7e64e052012-12-14 17:02:18 +01001139static void virtio_ccw_remove(struct ccw_device *cdev)
1140{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001141 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001142 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001143
Heinz Graalfse75279c2014-04-28 11:24:05 +09301144 if (vcdev && cdev->online) {
1145 if (vcdev->device_lost)
1146 virtio_break_device(&vcdev->vdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001147 unregister_virtio_device(&vcdev->vdev);
Heinz Graalfse75279c2014-04-28 11:24:05 +09301148 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1149 dev_set_drvdata(&cdev->dev, NULL);
1150 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1151 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001152 cdev->handler = NULL;
1153}
1154
1155static int virtio_ccw_offline(struct ccw_device *cdev)
1156{
Heinz Graalfs79629b22014-03-05 15:23:54 +01001157 unsigned long flags;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001158 struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001159
Heinz Graalfse75279c2014-04-28 11:24:05 +09301160 if (!vcdev)
1161 return 0;
1162 if (vcdev->device_lost)
1163 virtio_break_device(&vcdev->vdev);
1164 unregister_virtio_device(&vcdev->vdev);
1165 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1166 dev_set_drvdata(&cdev->dev, NULL);
1167 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001168 return 0;
1169}
1170
Thomas Huth6bb2c832014-10-07 16:39:50 +02001171static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
1172{
1173 struct virtio_rev_info *rev;
1174 struct ccw1 *ccw;
1175 int ret;
1176
1177 ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
1178 if (!ccw)
1179 return -ENOMEM;
1180 rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
1181 if (!rev) {
1182 kfree(ccw);
1183 return -ENOMEM;
1184 }
1185
1186 /* Set transport revision */
1187 ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
1188 ccw->flags = 0;
1189 ccw->count = sizeof(*rev);
1190 ccw->cda = (__u32)(unsigned long)rev;
1191
1192 vcdev->revision = VIRTIO_CCW_REV_MAX;
1193 do {
1194 rev->revision = vcdev->revision;
1195 /* none of our supported revisions carry payload */
1196 rev->length = 0;
1197 ret = ccw_io_helper(vcdev, ccw,
1198 VIRTIO_CCW_DOING_SET_VIRTIO_REV);
1199 if (ret == -EOPNOTSUPP) {
1200 if (vcdev->revision == 0)
1201 /*
1202 * The host device does not support setting
1203 * the revision: let's operate it in legacy
1204 * mode.
1205 */
1206 ret = 0;
1207 else
1208 vcdev->revision--;
1209 }
1210 } while (ret == -EOPNOTSUPP);
1211
1212 kfree(ccw);
1213 kfree(rev);
1214 return ret;
1215}
Cornelia Huck7e64e052012-12-14 17:02:18 +01001216
Cornelia Huck7e64e052012-12-14 17:02:18 +01001217static int virtio_ccw_online(struct ccw_device *cdev)
1218{
1219 int ret;
1220 struct virtio_ccw_device *vcdev;
Heinz Graalfs2e021042014-02-27 14:34:35 +01001221 unsigned long flags;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001222
1223 vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
1224 if (!vcdev) {
1225 dev_warn(&cdev->dev, "Could not get memory for virtio\n");
1226 ret = -ENOMEM;
1227 goto out_free;
1228 }
Cornelia Huck7e64e052012-12-14 17:02:18 +01001229 vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
1230 GFP_DMA | GFP_KERNEL);
1231 if (!vcdev->config_block) {
1232 ret = -ENOMEM;
1233 goto out_free;
1234 }
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001235 vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
1236 if (!vcdev->status) {
Cornelia Huck7e64e052012-12-14 17:02:18 +01001237 ret = -ENOMEM;
1238 goto out_free;
1239 }
1240
Cornelia Huck96b14532013-02-06 10:23:39 +01001241 vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
1242
Cornelia Huck7e64e052012-12-14 17:02:18 +01001243 vcdev->vdev.dev.parent = &cdev->dev;
1244 vcdev->vdev.dev.release = virtio_ccw_release_dev;
1245 vcdev->vdev.config = &virtio_ccw_config_ops;
1246 vcdev->cdev = cdev;
1247 init_waitqueue_head(&vcdev->wait_q);
1248 INIT_LIST_HEAD(&vcdev->virtqueues);
1249 spin_lock_init(&vcdev->lock);
1250
Heinz Graalfs2e021042014-02-27 14:34:35 +01001251 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001252 dev_set_drvdata(&cdev->dev, vcdev);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001253 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001254 vcdev->vdev.id.vendor = cdev->id.cu_type;
1255 vcdev->vdev.id.device = cdev->id.cu_model;
Thomas Huth6bb2c832014-10-07 16:39:50 +02001256
Michael S. Tsirkin2003a7a2015-04-15 10:17:44 +09301257 ret = virtio_ccw_set_transport_rev(vcdev);
1258 if (ret)
1259 goto out_free;
Thomas Huth6bb2c832014-10-07 16:39:50 +02001260
Cornelia Huck7e64e052012-12-14 17:02:18 +01001261 ret = register_virtio_device(&vcdev->vdev);
1262 if (ret) {
1263 dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
1264 ret);
1265 goto out_put;
1266 }
1267 return 0;
1268out_put:
Heinz Graalfs2e021042014-02-27 14:34:35 +01001269 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001270 dev_set_drvdata(&cdev->dev, NULL);
Heinz Graalfs2e021042014-02-27 14:34:35 +01001271 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001272 put_device(&vcdev->vdev.dev);
1273 return ret;
1274out_free:
1275 if (vcdev) {
Cornelia Huck73fa21e2013-01-07 15:51:51 +01001276 kfree(vcdev->status);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001277 kfree(vcdev->config_block);
Cornelia Huck7e64e052012-12-14 17:02:18 +01001278 }
1279 kfree(vcdev);
1280 return ret;
1281}
1282
1283static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
1284{
Heinz Graalfse75279c2014-04-28 11:24:05 +09301285 int rc;
1286 struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
1287
1288 /*
1289 * Make sure vcdev is set
1290 * i.e. set_offline/remove callback not already running
1291 */
1292 if (!vcdev)
1293 return NOTIFY_DONE;
1294
1295 switch (event) {
1296 case CIO_GONE:
1297 vcdev->device_lost = true;
1298 rc = NOTIFY_DONE;
1299 break;
1300 default:
1301 rc = NOTIFY_DONE;
1302 break;
1303 }
1304 return rc;
Cornelia Huck7e64e052012-12-14 17:02:18 +01001305}
1306
1307static struct ccw_device_id virtio_ids[] = {
1308 { CCW_DEVICE(0x3832, 0) },
1309 {},
1310};
Cornelia Huck7e64e052012-12-14 17:02:18 +01001311
1312static struct ccw_driver virtio_ccw_driver = {
1313 .driver = {
1314 .owner = THIS_MODULE,
1315 .name = "virtio_ccw",
1316 },
1317 .ids = virtio_ids,
1318 .probe = virtio_ccw_probe,
1319 .remove = virtio_ccw_remove,
1320 .set_offline = virtio_ccw_offline,
1321 .set_online = virtio_ccw_online,
1322 .notify = virtio_ccw_cio_notify,
Linus Torvalds89f88332013-02-24 13:07:18 -08001323 .int_class = IRQIO_VIR,
Cornelia Huck7e64e052012-12-14 17:02:18 +01001324};
1325
1326static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
1327 int max_digit, int max_val)
1328{
1329 int diff;
1330
1331 diff = 0;
1332 *val = 0;
1333
1334 while (diff <= max_digit) {
1335 int value = hex_to_bin(**cp);
1336
1337 if (value < 0)
1338 break;
1339 *val = *val * 16 + value;
1340 (*cp)++;
1341 diff++;
1342 }
1343
1344 if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
1345 return 1;
1346
1347 return 0;
1348}
1349
1350static int __init parse_busid(char *str, unsigned int *cssid,
1351 unsigned int *ssid, unsigned int *devno)
1352{
1353 char *str_work;
1354 int rc, ret;
1355
1356 rc = 1;
1357
1358 if (*str == '\0')
1359 goto out;
1360
1361 str_work = str;
1362 ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
1363 if (ret || (str_work[0] != '.'))
1364 goto out;
1365 str_work++;
1366 ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
1367 if (ret || (str_work[0] != '.'))
1368 goto out;
1369 str_work++;
1370 ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
1371 if (ret || (str_work[0] != '\0'))
1372 goto out;
1373
1374 rc = 0;
1375out:
1376 return rc;
1377}
1378
1379static void __init no_auto_parse(void)
1380{
1381 unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
1382 char *parm, *str;
1383 int rc;
1384
1385 str = no_auto;
1386 while ((parm = strsep(&str, ","))) {
1387 rc = parse_busid(strsep(&parm, "-"), &from_cssid,
1388 &from_ssid, &from);
1389 if (rc)
1390 continue;
1391 if (parm != NULL) {
1392 rc = parse_busid(parm, &to_cssid,
1393 &to_ssid, &to);
1394 if ((from_ssid > to_ssid) ||
1395 ((from_ssid == to_ssid) && (from > to)))
1396 rc = -EINVAL;
1397 } else {
1398 to_cssid = from_cssid;
1399 to_ssid = from_ssid;
1400 to = from;
1401 }
1402 if (rc)
1403 continue;
1404 while ((from_ssid < to_ssid) ||
1405 ((from_ssid == to_ssid) && (from <= to))) {
1406 set_bit(from, devs_no_auto[from_ssid]);
1407 from++;
1408 if (from > __MAX_SUBCHANNEL) {
1409 from_ssid++;
1410 from = 0;
1411 }
1412 }
1413 }
1414}
1415
1416static int __init virtio_ccw_init(void)
1417{
1418 /* parse no_auto string before we do anything further */
1419 no_auto_parse();
1420 return ccw_driver_register(&virtio_ccw_driver);
1421}
Paul Gortmakeracc50ec2016-10-30 16:37:32 -04001422device_initcall(virtio_ccw_init);