1
2
3
4
5
6
7#include <linux/ctype.h>
8#include <linux/debugfs.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/visorbus.h>
12#include <linux/uuid.h>
13
14#include "visorbus_private.h"
15
16static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
17
18
19#define LINESIZE 99
20#define POLLJIFFIES_NORMALCHANNEL 10
21
22
23static bool initialized;
24static struct dentry *visorbus_debugfs_dir;
25
26
27
28
29
30
31static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
32 char *buf)
33{
34 struct visor_device *vdev;
35 const guid_t *guid;
36
37 vdev = to_visor_device(dev);
38 guid = visorchannel_get_guid(vdev->visorchannel);
39 return sprintf(buf, "visorbus:%pUl\n", guid);
40}
41static DEVICE_ATTR_RO(modalias);
42
43static struct attribute *visorbus_dev_attrs[] = {
44 &dev_attr_modalias.attr,
45 NULL,
46};
47
48ATTRIBUTE_GROUPS(visorbus_dev);
49
50
51static struct visor_vbus_deviceinfo chipset_driverinfo;
52
53static struct visor_vbus_deviceinfo clientbus_driverinfo;
54
55
56static LIST_HEAD(list_all_bus_instances);
57
58static LIST_HEAD(list_all_device_instances);
59
60
61
62
63
64
65
66int visor_check_channel(struct channel_header *ch, struct device *dev,
67 const guid_t *expected_guid, char *chname,
68 u64 expected_min_bytes, u32 expected_version,
69 u64 expected_signature)
70{
71 if (!guid_is_null(expected_guid)) {
72
73 if (!guid_equal(&ch->chtype, expected_guid)) {
74 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
75 chname, expected_guid, expected_guid,
76 &ch->chtype);
77 return 0;
78 }
79 }
80
81 if (expected_min_bytes > 0) {
82 if (ch->size < expected_min_bytes) {
83 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
84 chname, expected_guid,
85 (unsigned long long)expected_min_bytes,
86 ch->size);
87 return 0;
88 }
89 }
90
91 if (expected_version > 0) {
92 if (ch->version_id != expected_version) {
93 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
94 chname, expected_guid,
95 (unsigned long)expected_version,
96 ch->version_id);
97 return 0;
98 }
99 }
100
101 if (expected_signature > 0) {
102 if (ch->signature != expected_signature) {
103 dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
104 chname, expected_guid, expected_signature,
105 ch->signature);
106 return 0;
107 }
108 }
109 return 1;
110}
111
112static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
113{
114 struct visor_device *dev;
115 const guid_t *guid;
116
117 dev = to_visor_device(xdev);
118 guid = visorchannel_get_guid(dev->visorchannel);
119 return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
120}
121
122
123
124
125
126
127
128
129
130
131static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
132{
133 const guid_t *channel_type;
134 int i;
135 struct visor_device *dev;
136 struct visor_driver *drv;
137 struct visorchannel *chan;
138
139 dev = to_visor_device(xdev);
140 channel_type = visorchannel_get_guid(dev->visorchannel);
141 drv = to_visor_driver(xdrv);
142 chan = dev->visorchannel;
143 if (!drv->channel_types)
144 return 0;
145 for (i = 0; !guid_is_null(&drv->channel_types[i].guid); i++)
146 if (guid_equal(&drv->channel_types[i].guid, channel_type) &&
147 visor_check_channel(visorchannel_get_header(chan),
148 xdev,
149 &drv->channel_types[i].guid,
150 (char *)drv->channel_types[i].name,
151 drv->channel_types[i].min_bytes,
152 drv->channel_types[i].version,
153 VISOR_CHANNEL_SIGNATURE))
154 return i + 1;
155 return 0;
156}
157
158
159
160
161
162static struct bus_type visorbus_type = {
163 .name = "visorbus",
164 .match = visorbus_match,
165 .uevent = visorbus_uevent,
166 .dev_groups = visorbus_dev_groups,
167};
168
169struct visor_busdev {
170 u32 bus_no;
171 u32 dev_no;
172};
173
174static int match_visorbus_dev_by_id(struct device *dev, const void *data)
175{
176 struct visor_device *vdev = to_visor_device(dev);
177 const struct visor_busdev *id = data;
178
179 if (vdev->chipset_bus_no == id->bus_no &&
180 vdev->chipset_dev_no == id->dev_no)
181 return 1;
182 return 0;
183}
184
185struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
186 struct visor_device *from)
187{
188 struct device *dev;
189 struct device *dev_start = NULL;
190 struct visor_busdev id = {
191 .bus_no = bus_no,
192 .dev_no = dev_no
193 };
194
195 if (from)
196 dev_start = &from->device;
197 dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
198 match_visorbus_dev_by_id);
199 if (!dev)
200 return NULL;
201 return to_visor_device(dev);
202}
203
204
205
206
207
208
209
210static void visorbus_release_busdevice(struct device *xdev)
211{
212 struct visor_device *dev = dev_get_drvdata(xdev);
213
214 debugfs_remove(dev->debugfs_bus_info);
215 debugfs_remove_recursive(dev->debugfs_dir);
216 visorchannel_destroy(dev->visorchannel);
217 kfree(dev);
218}
219
220
221
222
223
224
225static void visorbus_release_device(struct device *xdev)
226{
227 struct visor_device *dev = to_visor_device(xdev);
228
229 visorchannel_destroy(dev->visorchannel);
230 kfree(dev);
231}
232
233
234
235
236
237
238static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
239 char *buf)
240{
241 struct visor_device *vdev = to_visor_device(dev);
242
243 return sprintf(buf, "0x%llx\n",
244 visorchannel_get_physaddr(vdev->visorchannel));
245}
246static DEVICE_ATTR_RO(physaddr);
247
248static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
249 char *buf)
250{
251 struct visor_device *vdev = to_visor_device(dev);
252
253 return sprintf(buf, "0x%lx\n",
254 visorchannel_get_nbytes(vdev->visorchannel));
255}
256static DEVICE_ATTR_RO(nbytes);
257
258static ssize_t clientpartition_show(struct device *dev,
259 struct device_attribute *attr, char *buf)
260{
261 struct visor_device *vdev = to_visor_device(dev);
262
263 return sprintf(buf, "0x%llx\n",
264 visorchannel_get_clientpartition(vdev->visorchannel));
265}
266static DEVICE_ATTR_RO(clientpartition);
267
268static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
269 char *buf)
270{
271 struct visor_device *vdev = to_visor_device(dev);
272 char typeid[LINESIZE];
273
274 return sprintf(buf, "%s\n",
275 visorchannel_id(vdev->visorchannel, typeid));
276}
277static DEVICE_ATTR_RO(typeguid);
278
279static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
280 char *buf)
281{
282 struct visor_device *vdev = to_visor_device(dev);
283 char zoneid[LINESIZE];
284
285 return sprintf(buf, "%s\n",
286 visorchannel_zoneid(vdev->visorchannel, zoneid));
287}
288static DEVICE_ATTR_RO(zoneguid);
289
290static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
291 char *buf)
292{
293 int i = 0;
294 struct bus_type *xbus = dev->bus;
295 struct device_driver *xdrv = dev->driver;
296 struct visor_driver *drv = NULL;
297
298 if (!xdrv)
299 return 0;
300 i = xbus->match(dev, xdrv);
301 if (!i)
302 return 0;
303 drv = to_visor_driver(xdrv);
304 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
305}
306static DEVICE_ATTR_RO(typename);
307
308static struct attribute *channel_attrs[] = {
309 &dev_attr_physaddr.attr,
310 &dev_attr_nbytes.attr,
311 &dev_attr_clientpartition.attr,
312 &dev_attr_typeguid.attr,
313 &dev_attr_zoneguid.attr,
314 &dev_attr_typename.attr,
315 NULL
316};
317
318ATTRIBUTE_GROUPS(channel);
319
320
321
322
323
324
325
326static ssize_t partition_handle_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct visor_device *vdev = to_visor_device(dev);
330 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
331
332 return sprintf(buf, "0x%llx\n", handle);
333}
334static DEVICE_ATTR_RO(partition_handle);
335
336static ssize_t partition_guid_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 struct visor_device *vdev = to_visor_device(dev);
340
341 return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
342}
343static DEVICE_ATTR_RO(partition_guid);
344
345static ssize_t partition_name_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
348 struct visor_device *vdev = to_visor_device(dev);
349
350 return sprintf(buf, "%s\n", vdev->name);
351}
352static DEVICE_ATTR_RO(partition_name);
353
354static ssize_t channel_addr_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
357 struct visor_device *vdev = to_visor_device(dev);
358 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
359
360 return sprintf(buf, "0x%llx\n", addr);
361}
362static DEVICE_ATTR_RO(channel_addr);
363
364static ssize_t channel_bytes_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct visor_device *vdev = to_visor_device(dev);
368 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
369
370 return sprintf(buf, "0x%llx\n", nbytes);
371}
372static DEVICE_ATTR_RO(channel_bytes);
373
374static ssize_t channel_id_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
376{
377 struct visor_device *vdev = to_visor_device(dev);
378 int len = 0;
379
380 visorchannel_id(vdev->visorchannel, buf);
381 len = strlen(buf);
382 buf[len++] = '\n';
383 return len;
384}
385static DEVICE_ATTR_RO(channel_id);
386
387static struct attribute *visorbus_attrs[] = {
388 &dev_attr_partition_handle.attr,
389 &dev_attr_partition_guid.attr,
390 &dev_attr_partition_name.attr,
391 &dev_attr_channel_addr.attr,
392 &dev_attr_channel_bytes.attr,
393 &dev_attr_channel_id.attr,
394 NULL
395};
396
397ATTRIBUTE_GROUPS(visorbus);
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
417 struct seq_file *seq, int devix)
418{
419
420 if (!isprint(devinfo->devtype[0]))
421 return;
422 if (devix >= 0)
423 seq_printf(seq, "[%d]", devix);
424 else
425
426 seq_puts(seq, " ");
427
428
429
430
431 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
432 (int)sizeof(devinfo->devtype), devinfo->devtype);
433 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
434 (int)sizeof(devinfo->drvname), devinfo->drvname);
435 seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
436 devinfo->infostrs);
437}
438
439static int bus_info_debugfs_show(struct seq_file *seq, void *v)
440{
441 int i = 0;
442 unsigned long off;
443 struct visor_vbus_deviceinfo dev_info;
444 struct visor_device *vdev = seq->private;
445 struct visorchannel *channel = vdev->visorchannel;
446
447 if (!channel)
448 return 0;
449
450 seq_printf(seq,
451 "Client device/driver info for %s partition (vbus #%u):\n",
452 ((vdev->name) ? (char *)(vdev->name) : ""),
453 vdev->chipset_bus_no);
454 if (visorchannel_read(channel,
455 offsetof(struct visor_vbus_channel, chp_info),
456 &dev_info, sizeof(dev_info)) >= 0)
457 vbuschannel_print_devinfo(&dev_info, seq, -1);
458 if (visorchannel_read(channel,
459 offsetof(struct visor_vbus_channel, bus_info),
460 &dev_info, sizeof(dev_info)) >= 0)
461 vbuschannel_print_devinfo(&dev_info, seq, -1);
462
463 off = offsetof(struct visor_vbus_channel, dev_info);
464 while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
465 if (visorchannel_read(channel, off, &dev_info,
466 sizeof(dev_info)) >= 0)
467 vbuschannel_print_devinfo(&dev_info, seq, i);
468 off += sizeof(dev_info);
469 i++;
470 }
471 return 0;
472}
473
474static int bus_info_debugfs_open(struct inode *inode, struct file *file)
475{
476 return single_open(file, bus_info_debugfs_show, inode->i_private);
477}
478
479static const struct file_operations bus_info_debugfs_fops = {
480 .owner = THIS_MODULE,
481 .open = bus_info_debugfs_open,
482 .read = seq_read,
483 .llseek = seq_lseek,
484 .release = single_release,
485};
486
487static void dev_periodic_work(struct timer_list *t)
488{
489 struct visor_device *dev = from_timer(dev, t, timer);
490 struct visor_driver *drv = to_visor_driver(dev->device.driver);
491
492 drv->channel_interrupt(dev);
493 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
494}
495
496static int dev_start_periodic_work(struct visor_device *dev)
497{
498 if (dev->being_removed || dev->timer_active)
499 return -EINVAL;
500
501
502 get_device(&dev->device);
503 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
504 add_timer(&dev->timer);
505 dev->timer_active = true;
506 return 0;
507}
508
509static void dev_stop_periodic_work(struct visor_device *dev)
510{
511 if (!dev->timer_active)
512 return;
513
514 del_timer_sync(&dev->timer);
515 dev->timer_active = false;
516 put_device(&dev->device);
517}
518
519
520
521
522
523
524
525
526
527
528
529static int visordriver_remove_device(struct device *xdev)
530{
531 struct visor_device *dev = to_visor_device(xdev);
532 struct visor_driver *drv = to_visor_driver(xdev->driver);
533
534 mutex_lock(&dev->visordriver_callback_lock);
535 dev->being_removed = true;
536 drv->remove(dev);
537 mutex_unlock(&dev->visordriver_callback_lock);
538 dev_stop_periodic_work(dev);
539 put_device(&dev->device);
540 return 0;
541}
542
543
544
545
546
547
548
549
550void visorbus_unregister_visor_driver(struct visor_driver *drv)
551{
552 driver_unregister(&drv->driver);
553}
554EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
555
556
557
558
559
560
561
562
563
564
565
566
567
568int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
569 void *dest, unsigned long nbytes)
570{
571 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
572}
573EXPORT_SYMBOL_GPL(visorbus_read_channel);
574
575
576
577
578
579
580
581
582
583
584
585
586
587int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
588 void *src, unsigned long nbytes)
589{
590 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
591}
592EXPORT_SYMBOL_GPL(visorbus_write_channel);
593
594
595
596
597
598
599
600
601
602int visorbus_enable_channel_interrupts(struct visor_device *dev)
603{
604 struct visor_driver *drv = to_visor_driver(dev->device.driver);
605
606 if (!drv->channel_interrupt) {
607 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
608 return -ENOENT;
609 }
610
611 return dev_start_periodic_work(dev);
612}
613EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
614
615
616
617
618
619
620void visorbus_disable_channel_interrupts(struct visor_device *dev)
621{
622 dev_stop_periodic_work(dev);
623}
624EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648int create_visor_device(struct visor_device *dev)
649{
650 int err;
651 u32 chipset_bus_no = dev->chipset_bus_no;
652 u32 chipset_dev_no = dev->chipset_dev_no;
653
654 mutex_init(&dev->visordriver_callback_lock);
655 dev->device.bus = &visorbus_type;
656 dev->device.groups = channel_groups;
657 device_initialize(&dev->device);
658 dev->device.release = visorbus_release_device;
659
660 get_device(&dev->device);
661 timer_setup(&dev->timer, dev_periodic_work, 0);
662
663
664
665
666
667 err = dev_set_name(&dev->device, "vbus%u:dev%u",
668 chipset_bus_no, chipset_dev_no);
669 if (err)
670 goto err_put;
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688 err = device_add(&dev->device);
689 if (err < 0)
690 goto err_put;
691 list_add_tail(&dev->list_all, &list_all_device_instances);
692 dev->state.created = 1;
693 visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
694
695 return 0;
696
697err_put:
698 put_device(&dev->device);
699 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
700 return err;
701}
702
703void remove_visor_device(struct visor_device *dev)
704{
705 list_del(&dev->list_all);
706 put_device(&dev->device);
707 if (dev->pending_msg_hdr)
708 visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
709 device_unregister(&dev->device);
710}
711
712static int get_vbus_header_info(struct visorchannel *chan,
713 struct device *dev,
714 struct visor_vbus_headerinfo *hdr_info)
715{
716 int err;
717
718 if (!visor_check_channel(visorchannel_get_header(chan),
719 dev,
720 &visor_vbus_channel_guid,
721 "vbus",
722 sizeof(struct visor_vbus_channel),
723 VISOR_VBUS_CHANNEL_VERSIONID,
724 VISOR_CHANNEL_SIGNATURE))
725 return -EINVAL;
726
727 err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
728 sizeof(*hdr_info));
729 if (err < 0)
730 return err;
731 if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
732 return -EINVAL;
733 if (hdr_info->device_info_struct_bytes <
734 sizeof(struct visor_vbus_deviceinfo))
735 return -EINVAL;
736 return 0;
737}
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752static void write_vbus_chp_info(struct visorchannel *chan,
753 struct visor_vbus_headerinfo *hdr_info,
754 struct visor_vbus_deviceinfo *info)
755{
756 int off;
757
758 if (hdr_info->chp_info_offset == 0)
759 return;
760
761 off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
762 visorchannel_write(chan, off, info, sizeof(*info));
763}
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778static void write_vbus_bus_info(struct visorchannel *chan,
779 struct visor_vbus_headerinfo *hdr_info,
780 struct visor_vbus_deviceinfo *info)
781{
782 int off;
783
784 if (hdr_info->bus_info_offset == 0)
785 return;
786
787 off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
788 visorchannel_write(chan, off, info, sizeof(*info));
789}
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805static void write_vbus_dev_info(struct visorchannel *chan,
806 struct visor_vbus_headerinfo *hdr_info,
807 struct visor_vbus_deviceinfo *info,
808 unsigned int devix)
809{
810 int off;
811
812 if (hdr_info->dev_info_offset == 0)
813 return;
814 off = (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
815 (hdr_info->device_info_struct_bytes * devix);
816 visorchannel_write(chan, off, info, sizeof(*info));
817}
818
819static void bus_device_info_init(
820 struct visor_vbus_deviceinfo *bus_device_info_ptr,
821 const char *dev_type, const char *drv_name)
822{
823 memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
824 snprintf(bus_device_info_ptr->devtype,
825 sizeof(bus_device_info_ptr->devtype),
826 "%s", (dev_type) ? dev_type : "unknownType");
827 snprintf(bus_device_info_ptr->drvname,
828 sizeof(bus_device_info_ptr->drvname),
829 "%s", (drv_name) ? drv_name : "unknownDriver");
830 snprintf(bus_device_info_ptr->infostrs,
831 sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
832 utsname()->release);
833}
834
835
836
837
838
839
840
841
842static void publish_vbus_dev_info(struct visor_device *visordev)
843{
844 int i;
845 struct visor_device *bdev;
846 struct visor_driver *visordrv;
847 u32 bus_no = visordev->chipset_bus_no;
848 u32 dev_no = visordev->chipset_dev_no;
849 struct visor_vbus_deviceinfo dev_info;
850 const char *chan_type_name = NULL;
851 struct visor_vbus_headerinfo *hdr_info;
852
853 if (!visordev->device.driver)
854 return;
855 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
856 if (!bdev)
857 return;
858 hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
859 if (!hdr_info)
860 return;
861 visordrv = to_visor_driver(visordev->device.driver);
862
863
864
865
866
867
868
869 for (i = 0; visordrv->channel_types[i].name; i++) {
870 if (guid_equal(&visordrv->channel_types[i].guid,
871 &visordev->channel_type_guid)) {
872 chan_type_name = visordrv->channel_types[i].name;
873 break;
874 }
875 }
876 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
877 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
878 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
879 write_vbus_bus_info(bdev->visorchannel, hdr_info,
880 &clientbus_driverinfo);
881}
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898static int visordriver_probe_device(struct device *xdev)
899{
900 int err;
901 struct visor_driver *drv = to_visor_driver(xdev->driver);
902 struct visor_device *dev = to_visor_device(xdev);
903
904 mutex_lock(&dev->visordriver_callback_lock);
905 dev->being_removed = false;
906 err = drv->probe(dev);
907 if (err) {
908 mutex_unlock(&dev->visordriver_callback_lock);
909 return err;
910 }
911
912 get_device(&dev->device);
913 publish_vbus_dev_info(dev);
914 mutex_unlock(&dev->visordriver_callback_lock);
915 return 0;
916}
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966int visorbus_register_visor_driver(struct visor_driver *drv)
967{
968
969 if (!initialized)
970 return -ENODEV;
971 if (!drv->probe)
972 return -EINVAL;
973 if (!drv->remove)
974 return -EINVAL;
975 if (!drv->pause)
976 return -EINVAL;
977 if (!drv->resume)
978 return -EINVAL;
979
980 drv->driver.name = drv->name;
981 drv->driver.bus = &visorbus_type;
982 drv->driver.probe = visordriver_probe_device;
983 drv->driver.remove = visordriver_remove_device;
984 drv->driver.owner = drv->owner;
985
986
987
988
989
990
991
992
993
994
995
996
997 return driver_register(&drv->driver);
998}
999EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1000
1001
1002
1003
1004
1005
1006
1007
1008int visorbus_create_instance(struct visor_device *dev)
1009{
1010 int id = dev->chipset_bus_no;
1011 int err;
1012 struct visor_vbus_headerinfo *hdr_info;
1013
1014 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1015 if (!hdr_info)
1016 return -ENOMEM;
1017 dev_set_name(&dev->device, "visorbus%d", id);
1018 dev->device.bus = &visorbus_type;
1019 dev->device.groups = visorbus_groups;
1020 dev->device.release = visorbus_release_busdevice;
1021 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1022 visorbus_debugfs_dir);
1023 dev->debugfs_bus_info = debugfs_create_file("client_bus_info", 0440,
1024 dev->debugfs_dir, dev,
1025 &bus_info_debugfs_fops);
1026 dev_set_drvdata(&dev->device, dev);
1027 err = get_vbus_header_info(dev->visorchannel, &dev->device, hdr_info);
1028 if (err < 0)
1029 goto err_debugfs_dir;
1030 err = device_register(&dev->device);
1031 if (err < 0)
1032 goto err_debugfs_dir;
1033 list_add_tail(&dev->list_all, &list_all_bus_instances);
1034 dev->state.created = 1;
1035 dev->vbus_hdr_info = (void *)hdr_info;
1036 write_vbus_chp_info(dev->visorchannel, hdr_info, &chipset_driverinfo);
1037 write_vbus_bus_info(dev->visorchannel, hdr_info, &clientbus_driverinfo);
1038 visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
1039 return 0;
1040
1041err_debugfs_dir:
1042 debugfs_remove_recursive(dev->debugfs_dir);
1043 kfree(hdr_info);
1044 dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1045 return err;
1046}
1047
1048
1049
1050
1051
1052void visorbus_remove_instance(struct visor_device *dev)
1053{
1054
1055
1056
1057
1058
1059
1060
1061
1062 kfree(dev->vbus_hdr_info);
1063 list_del(&dev->list_all);
1064 if (dev->pending_msg_hdr)
1065 visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
1066 device_unregister(&dev->device);
1067}
1068
1069
1070
1071
1072static void remove_all_visor_devices(void)
1073{
1074 struct list_head *listentry, *listtmp;
1075
1076 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1077 struct visor_device *dev;
1078
1079 dev = list_entry(listentry, struct visor_device, list_all);
1080 remove_visor_device(dev);
1081 }
1082}
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093static void pause_state_change_complete(struct visor_device *dev, int status)
1094{
1095 if (!dev->pausing)
1096 return;
1097
1098 dev->pausing = false;
1099 visorbus_device_changestate_response(dev, status,
1100 segment_state_standby);
1101}
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112static void resume_state_change_complete(struct visor_device *dev, int status)
1113{
1114 if (!dev->resuming)
1115 return;
1116
1117 dev->resuming = false;
1118
1119
1120
1121
1122
1123 visorbus_device_changestate_response(dev, status,
1124 segment_state_running);
1125}
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1139 bool is_pause)
1140{
1141 int err;
1142 struct visor_driver *drv;
1143
1144
1145 if (!dev->device.driver)
1146 return 0;
1147 if (dev->pausing || dev->resuming)
1148 return -EBUSY;
1149
1150 drv = to_visor_driver(dev->device.driver);
1151 if (is_pause) {
1152 dev->pausing = true;
1153 err = drv->pause(dev, pause_state_change_complete);
1154 } else {
1155
1156
1157
1158
1159 publish_vbus_dev_info(dev);
1160 dev->resuming = true;
1161 err = drv->resume(dev, resume_state_change_complete);
1162 }
1163 return err;
1164}
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174int visorchipset_device_pause(struct visor_device *dev_info)
1175{
1176 int err;
1177
1178 err = visorchipset_initiate_device_pause_resume(dev_info, true);
1179 if (err < 0) {
1180 dev_info->pausing = false;
1181 return err;
1182 }
1183 return 0;
1184}
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194int visorchipset_device_resume(struct visor_device *dev_info)
1195{
1196 int err;
1197
1198 err = visorchipset_initiate_device_pause_resume(dev_info, false);
1199 if (err < 0) {
1200 dev_info->resuming = false;
1201 return err;
1202 }
1203 return 0;
1204}
1205
1206int visorbus_init(void)
1207{
1208 int err;
1209
1210 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1211 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1212 err = bus_register(&visorbus_type);
1213 if (err < 0)
1214 return err;
1215 initialized = true;
1216 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1217 return 0;
1218}
1219
1220void visorbus_exit(void)
1221{
1222 struct list_head *listentry, *listtmp;
1223
1224 remove_all_visor_devices();
1225 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1226 struct visor_device *dev;
1227
1228 dev = list_entry(listentry, struct visor_device, list_all);
1229 visorbus_remove_instance(dev);
1230 }
1231 bus_unregister(&visorbus_type);
1232 initialized = false;
1233 debugfs_remove_recursive(visorbus_debugfs_dir);
1234}
1235