staging: unisys: make visorbus_dev_groups static
[pandora-kernel.git] / drivers / staging / unisys / visorbus / visorbus_main.c
1 /* visorbus_main.c
2  *
3  * Copyright � 2010 - 2015 UNISYS CORPORATION
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13  * NON INFRINGEMENT.  See the GNU General Public License for more
14  * details.
15  */
16
17 #include <linux/uuid.h>
18
19 #include "visorbus.h"
20 #include "visorbus_private.h"
21 #include "version.h"
22 #include "periodic_work.h"
23 #include "vbuschannel.h"
24 #include "guestlinuxdebug.h"
25 #include "vmcallinterface.h"
26
27 #define MYDRVNAME "visorbus"
28
29 /* module parameters */
30 static int visorbus_debug;
31 static int visorbus_forcematch;
32 static int visorbus_forcenomatch;
33 static int visorbus_debugref;
34 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
35
36 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
37 #define POLLJIFFIES_TESTWORK         100
38 #define POLLJIFFIES_NORMALCHANNEL     10
39
40 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
41 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
42 static void fix_vbus_dev_info(struct visor_device *visordev);
43
44 /*  BUS type attributes
45  *
46  *  define & implement display of bus attributes under
47  *  /sys/bus/visorbus.
48  *
49  */
50
51 static ssize_t version_show(struct bus_type *bus, char *buf)
52 {
53         return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
54 }
55
56 static BUS_ATTR_RO(version);
57
58 static struct attribute *visorbus_bus_attrs[] = {
59         &bus_attr_version.attr,
60         NULL,
61 };
62
63 static const struct attribute_group visorbus_bus_group = {
64         .attrs = visorbus_bus_attrs,
65 };
66
67 static const struct attribute_group *visorbus_bus_groups[] = {
68         &visorbus_bus_group,
69         NULL,
70 };
71
72 /*
73  * DEVICE type attributes
74  *
75  * The modalias file will contain the guid of the device.
76  */
77 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
78                              char *buf)
79 {
80         struct visor_device *vdev;
81         uuid_le guid;
82
83         vdev = to_visor_device(dev);
84         guid = visorchannel_get_uuid(vdev->visorchannel);
85         return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
86 }
87 static DEVICE_ATTR_RO(modalias);
88
89 static struct attribute *visorbus_dev_attrs[] = {
90         &dev_attr_modalias.attr,
91         NULL,
92 };
93
94 /* sysfs example for bridge-only sysfs files using device_type's */
95 static const struct attribute_group visorbus_dev_group = {
96         .attrs = visorbus_dev_attrs,
97 };
98
99 static const struct attribute_group *visorbus_dev_groups[] = {
100         &visorbus_dev_group,
101         NULL,
102 };
103
104 /** This describes the TYPE of bus.
105  *  (Don't confuse this with an INSTANCE of the bus.)
106  */
107 struct bus_type visorbus_type = {
108         .name = "visorbus",
109         .match = visorbus_match,
110         .uevent = visorbus_uevent,
111         .dev_groups = visorbus_dev_groups,
112         .bus_groups = visorbus_bus_groups,
113 };
114
115 static struct delayed_work periodic_work;
116
117 /* YES, we need 2 workqueues.
118  * The reason is, workitems on the test queue may need to cancel
119  * workitems on the other queue.  You will be in for trouble if you try to
120  * do this with workitems queued on the same workqueue.
121  */
122 static struct workqueue_struct *periodic_test_workqueue;
123 static struct workqueue_struct *periodic_dev_workqueue;
124 static long long bus_count;     /** number of bus instances */
125                                         /** ever-increasing */
126
127 static void chipset_bus_create(struct visor_device *bus_info);
128 static void chipset_bus_destroy(struct visor_device *bus_info);
129 static void chipset_device_create(struct visor_device *dev_info);
130 static void chipset_device_destroy(struct visor_device *dev_info);
131 static void chipset_device_pause(struct visor_device *dev_info);
132 static void chipset_device_resume(struct visor_device *dev_info);
133
134 /** These functions are implemented herein, and are called by the chipset
135  *  driver to notify us about specific events.
136  */
137 static struct visorchipset_busdev_notifiers chipset_notifiers = {
138         .bus_create = chipset_bus_create,
139         .bus_destroy = chipset_bus_destroy,
140         .device_create = chipset_device_create,
141         .device_destroy = chipset_device_destroy,
142         .device_pause = chipset_device_pause,
143         .device_resume = chipset_device_resume,
144 };
145
146 /** These functions are implemented in the chipset driver, and we call them
147  *  herein when we want to acknowledge a specific event.
148  */
149 static struct visorchipset_busdev_responders chipset_responders;
150
151 /* filled in with info about parent chipset driver when we register with it */
152 static struct ultra_vbus_deviceinfo chipset_driverinfo;
153 /* filled in with info about this driver, wrt it servicing client busses */
154 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
155
156 /** list of visor_device structs, linked via .list_all */
157 static LIST_HEAD(list_all_bus_instances);
158 /** list of visor_device structs, linked via .list_all */
159 static LIST_HEAD(list_all_device_instances);
160
161 static int
162 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
163 {
164         struct visor_device *dev;
165         uuid_le guid;
166
167         dev = to_visor_device(xdev);
168         guid = visorchannel_get_uuid(dev->visorchannel);
169
170         if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
171                 return -ENOMEM;
172         return 0;
173 }
174
175 /* This is called automatically upon adding a visor_device (device_add), or
176  * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
177  * provided driver can control the specified device.
178  */
179 static int
180 visorbus_match(struct device *xdev, struct device_driver *xdrv)
181 {
182         uuid_le channel_type;
183         int rc = 0;
184         int i;
185         struct visor_device *dev;
186         struct visor_driver *drv;
187
188         dev = to_visor_device(xdev);
189         drv = to_visor_driver(xdrv);
190         channel_type = visorchannel_get_uuid(dev->visorchannel);
191         if (visorbus_forcematch) {
192                 rc = 1;
193                 goto away;
194         }
195         if (visorbus_forcenomatch)
196                 goto away;
197
198         if (!drv->channel_types)
199                 goto away;
200         for (i = 0;
201              (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
202              (drv->channel_types[i].name);
203              i++)
204                 if (uuid_le_cmp(drv->channel_types[i].guid,
205                                 channel_type) == 0) {
206                         rc = i + 1;
207                         goto away;
208                 }
209 away:
210         return rc;
211 }
212
213 /** This is called when device_unregister() is called for the bus device
214  *  instance, after all other tasks involved with destroying the device
215  *  are complete.
216  */
217 static void
218 visorbus_release_busdevice(struct device *xdev)
219 {
220         struct visor_device *dev = dev_get_drvdata(xdev);
221
222         dev_set_drvdata(xdev, NULL);
223         kfree(dev);
224 }
225
226 /** This is called when device_unregister() is called for each child
227  *  device instance.
228  */
229 static void
230 visorbus_release_device(struct device *xdev)
231 {
232         struct visor_device *dev = to_visor_device(xdev);
233
234         if (dev->periodic_work) {
235                 visor_periodic_work_destroy(dev->periodic_work);
236                 dev->periodic_work = NULL;
237         }
238         if (dev->visorchannel) {
239                 visorchannel_destroy(dev->visorchannel);
240                 dev->visorchannel = NULL;
241         }
242         kfree(dev);
243 }
244
245 /* Implement publishing of device node attributes under:
246  *
247  *     /sys/bus/visorbus<x>/dev<y>/devmajorminor
248  *
249  */
250
251 #define to_devmajorminor_attr(_attr) \
252         container_of(_attr, struct devmajorminor_attribute, attr)
253 #define to_visor_device_from_kobjdevmajorminor(obj) \
254         container_of(obj, struct visor_device, kobjdevmajorminor)
255
256 struct devmajorminor_attribute {
257         struct attribute attr;
258         int slot;
259         ssize_t (*show)(struct visor_device *, int slot, char *buf);
260         ssize_t (*store)(struct visor_device *, int slot, const char *buf,
261                          size_t count);
262 };
263
264 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
265 {
266         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
267
268         if (slot < 0 || slot >= maxdevnodes)
269                 return 0;
270         return snprintf(buf, PAGE_SIZE, "%d:%d\n",
271                         dev->devnodes[slot].major, dev->devnodes[slot].minor);
272 }
273
274 static ssize_t
275 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
276 {
277         struct devmajorminor_attribute *devmajorminor_attr =
278             to_devmajorminor_attr(attr);
279         struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
280         ssize_t ret = 0;
281
282         if (devmajorminor_attr->show)
283                 ret = devmajorminor_attr->show(dev,
284                                                devmajorminor_attr->slot, buf);
285         return ret;
286 }
287
288 static ssize_t
289 devmajorminor_attr_store(struct kobject *kobj,
290                          struct attribute *attr, const char *buf, size_t count)
291 {
292         struct devmajorminor_attribute *devmajorminor_attr =
293             to_devmajorminor_attr(attr);
294         struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
295         ssize_t ret = 0;
296
297         if (devmajorminor_attr->store)
298                 ret = devmajorminor_attr->store(dev,
299                                                 devmajorminor_attr->slot,
300                                                 buf, count);
301         return ret;
302 }
303
304 static int register_devmajorminor_attributes(struct visor_device *dev);
305
306 static int
307 devmajorminor_create_file(struct visor_device *dev, const char *name,
308                           int major, int minor)
309 {
310         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
311         struct devmajorminor_attribute *myattr = NULL;
312         int x = -1, rc = 0, slot = -1;
313
314         register_devmajorminor_attributes(dev);
315         for (slot = 0; slot < maxdevnodes; slot++)
316                 if (!dev->devnodes[slot].attr)
317                         break;
318         if (slot == maxdevnodes) {
319                 rc = -ENOMEM;
320                 goto away;
321         }
322         myattr = kzalloc(sizeof(*myattr), GFP_KERNEL);
323         if (!myattr) {
324                 rc = -ENOMEM;
325                 goto away;
326         }
327         myattr->show = DEVMAJORMINOR_ATTR;
328         myattr->store = NULL;
329         myattr->slot = slot;
330         myattr->attr.name = name;
331         myattr->attr.mode = S_IRUGO;
332         dev->devnodes[slot].attr = myattr;
333         dev->devnodes[slot].major = major;
334         dev->devnodes[slot].minor = minor;
335         x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
336         if (x < 0) {
337                 rc = x;
338                 goto away;
339         }
340         kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
341 away:
342         if (rc < 0) {
343                 kfree(myattr);
344                 myattr = NULL;
345                 dev->devnodes[slot].attr = NULL;
346         }
347         return rc;
348 }
349
350 static void
351 devmajorminor_remove_file(struct visor_device *dev, int slot)
352 {
353         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
354         struct devmajorminor_attribute *myattr = NULL;
355
356         if (slot < 0 || slot >= maxdevnodes)
357                 return;
358         myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
359         if (!myattr)
360                 return;
361         sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
362         kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
363         dev->devnodes[slot].attr = NULL;
364         kfree(myattr);
365 }
366
367 static void
368 devmajorminor_remove_all_files(struct visor_device *dev)
369 {
370         int i = 0;
371         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
372
373         for (i = 0; i < maxdevnodes; i++)
374                 devmajorminor_remove_file(dev, i);
375 }
376
377 static const struct sysfs_ops devmajorminor_sysfs_ops = {
378         .show = devmajorminor_attr_show,
379         .store = devmajorminor_attr_store,
380 };
381
382 static struct kobj_type devmajorminor_kobj_type = {
383         .sysfs_ops = &devmajorminor_sysfs_ops
384 };
385
386 static int
387 register_devmajorminor_attributes(struct visor_device *dev)
388 {
389         int rc = 0, x = 0;
390
391         if (dev->kobjdevmajorminor.parent)
392                 goto away;      /* already registered */
393         x = kobject_init_and_add(&dev->kobjdevmajorminor,
394                                  &devmajorminor_kobj_type, &dev->device.kobj,
395                                  "devmajorminor");
396         if (x < 0) {
397                 rc = x;
398                 goto away;
399         }
400
401         kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
402
403 away:
404         return rc;
405 }
406
407 static void
408 unregister_devmajorminor_attributes(struct visor_device *dev)
409 {
410         if (!dev->kobjdevmajorminor.parent)
411                 return;         /* already unregistered */
412         devmajorminor_remove_all_files(dev);
413
414         kobject_del(&dev->kobjdevmajorminor);
415         kobject_put(&dev->kobjdevmajorminor);
416         dev->kobjdevmajorminor.parent = NULL;
417 }
418
419 /* begin implementation of specific channel attributes to appear under
420 * /sys/bus/visorbus<x>/dev<y>/channel
421 */
422 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
423                              char *buf)
424 {
425         struct visor_device *vdev = to_visor_device(dev);
426
427         if (!vdev->visorchannel)
428                 return 0;
429         return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
430                         visorchannel_get_physaddr(vdev->visorchannel));
431 }
432
433 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
434                            char *buf)
435 {
436         struct visor_device *vdev = to_visor_device(dev);
437
438         if (!vdev->visorchannel)
439                 return 0;
440         return snprintf(buf, PAGE_SIZE, "0x%lx\n",
441                         visorchannel_get_nbytes(vdev->visorchannel));
442 }
443
444 static ssize_t clientpartition_show(struct device *dev,
445                                     struct device_attribute *attr, char *buf)
446 {
447         struct visor_device *vdev = to_visor_device(dev);
448
449         if (!vdev->visorchannel)
450                 return 0;
451         return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
452                         visorchannel_get_clientpartition(vdev->visorchannel));
453 }
454
455 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
456                              char *buf)
457 {
458         struct visor_device *vdev = to_visor_device(dev);
459         char s[99];
460
461         if (!vdev->visorchannel)
462                 return 0;
463         return snprintf(buf, PAGE_SIZE, "%s\n",
464                         visorchannel_id(vdev->visorchannel, s));
465 }
466
467 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
468                              char *buf)
469 {
470         struct visor_device *vdev = to_visor_device(dev);
471         char s[99];
472
473         if (!vdev->visorchannel)
474                 return 0;
475         return snprintf(buf, PAGE_SIZE, "%s\n",
476                         visorchannel_zoneid(vdev->visorchannel, s));
477 }
478
479 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
480                              char *buf)
481 {
482         struct visor_device *vdev = to_visor_device(dev);
483         int i = 0;
484         struct bus_type *xbus = dev->bus;
485         struct device_driver *xdrv = dev->driver;
486         struct visor_driver *drv = NULL;
487
488         if (!vdev->visorchannel || !xbus || !xdrv)
489                 return 0;
490         i = xbus->match(dev, xdrv);
491         if (!i)
492                 return 0;
493         drv = to_visor_driver(xdrv);
494         return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
495 }
496
497 static DEVICE_ATTR_RO(physaddr);
498 static DEVICE_ATTR_RO(nbytes);
499 static DEVICE_ATTR_RO(clientpartition);
500 static DEVICE_ATTR_RO(typeguid);
501 static DEVICE_ATTR_RO(zoneguid);
502 static DEVICE_ATTR_RO(typename);
503
504 static struct attribute *channel_attrs[] = {
505                 &dev_attr_physaddr.attr,
506                 &dev_attr_nbytes.attr,
507                 &dev_attr_clientpartition.attr,
508                 &dev_attr_typeguid.attr,
509                 &dev_attr_zoneguid.attr,
510                 &dev_attr_typename.attr,
511                 NULL
512 };
513
514 static struct attribute_group channel_attr_grp = {
515                 .name = "channel",
516                 .attrs = channel_attrs,
517 };
518
519 static const struct attribute_group *visorbus_channel_groups[] = {
520                 &channel_attr_grp,
521                 NULL
522 };
523
524 /* end implementation of specific channel attributes */
525
526 /*  BUS instance attributes
527  *
528  *  define & implement display of bus attributes under
529  *  /sys/bus/visorbus/busses/visorbus<n>.
530  *
531  *  This is a bit hoaky because the kernel does not yet have the infrastructure
532  *  to separate bus INSTANCE attributes from bus TYPE attributes...
533  *  so we roll our own.  See businst.c / businst.h.
534  *
535  */
536
537 static ssize_t partition_handle_show(struct device *dev,
538                                      struct device_attribute *attr,
539                                      char *buf) {
540         struct visor_device *vdev = to_visor_device(dev);
541         u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
542
543         return snprintf(buf, PAGE_SIZE, "0x%Lx\n", handle);
544 }
545
546 static ssize_t partition_guid_show(struct device *dev,
547                                    struct device_attribute *attr,
548                                    char *buf) {
549         struct visor_device *vdev = to_visor_device(dev);
550
551         return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
552 }
553
554 static ssize_t partition_name_show(struct device *dev,
555                                    struct device_attribute *attr,
556                                    char *buf) {
557         struct visor_device *vdev = to_visor_device(dev);
558
559         return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
560 }
561
562 static ssize_t channel_addr_show(struct device *dev,
563                                  struct device_attribute *attr,
564                                  char *buf) {
565         struct visor_device *vdev = to_visor_device(dev);
566         u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
567
568         return snprintf(buf, PAGE_SIZE, "0x%Lx\n", addr);
569 }
570
571 static ssize_t channel_bytes_show(struct device *dev,
572                                   struct device_attribute *attr,
573                                   char *buf) {
574         struct visor_device *vdev = to_visor_device(dev);
575         u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
576
577         return snprintf(buf, PAGE_SIZE, "0x%Lx\n", nbytes);
578 }
579
580 static ssize_t channel_id_show(struct device *dev,
581                                struct device_attribute *attr,
582                                char *buf) {
583         struct visor_device *vdev = to_visor_device(dev);
584         int len = 0;
585
586         if (vdev->visorchannel) {
587                 visorchannel_id(vdev->visorchannel, buf);
588                 len = strlen(buf);
589                 buf[len++] = '\n';
590         }
591         return len;
592 }
593
594 static ssize_t client_bus_info_show(struct device *dev,
595                                     struct device_attribute *attr,
596                                     char *buf) {
597         struct visor_device *vdev = to_visor_device(dev);
598         struct visorchannel *channel = vdev->visorchannel;
599
600         int i, x, remain = PAGE_SIZE;
601         unsigned long off;
602         char *p = buf;
603         u8 *partition_name;
604         struct ultra_vbus_deviceinfo dev_info;
605
606         partition_name = "";
607         if (channel) {
608                 if (vdev->name)
609                         partition_name = vdev->name;
610                 x = snprintf(p, remain,
611                              "Client device / client driver info for %s partition (vbus #%d):\n",
612                              partition_name, vdev->chipset_dev_no);
613                 p += x;
614                 remain -= x;
615                 x = visorchannel_read(channel,
616                                       offsetof(struct
617                                                spar_vbus_channel_protocol,
618                                                chp_info),
619                                       &dev_info, sizeof(dev_info));
620                 if (x >= 0) {
621                         x = vbuschannel_devinfo_to_string(&dev_info, p,
622                                                           remain, -1);
623                         p += x;
624                         remain -= x;
625                 }
626                 x = visorchannel_read(channel,
627                                       offsetof(struct
628                                                spar_vbus_channel_protocol,
629                                                bus_info),
630                                       &dev_info, sizeof(dev_info));
631                 if (x >= 0) {
632                         x = vbuschannel_devinfo_to_string(&dev_info, p,
633                                                           remain, -1);
634                         p += x;
635                         remain -= x;
636                 }
637                 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
638                 i = 0;
639                 while (off + sizeof(dev_info) <=
640                        visorchannel_get_nbytes(channel)) {
641                         x = visorchannel_read(channel,
642                                               off, &dev_info, sizeof(dev_info));
643                         if (x >= 0) {
644                                 x = vbuschannel_devinfo_to_string
645                                     (&dev_info, p, remain, i);
646                                 p += x;
647                                 remain -= x;
648                         }
649                         off += sizeof(dev_info);
650                         i++;
651                 }
652         }
653         return PAGE_SIZE - remain;
654 }
655
656 static DEVICE_ATTR_RO(partition_handle);
657 static DEVICE_ATTR_RO(partition_guid);
658 static DEVICE_ATTR_RO(partition_name);
659 static DEVICE_ATTR_RO(channel_addr);
660 static DEVICE_ATTR_RO(channel_bytes);
661 static DEVICE_ATTR_RO(channel_id);
662 static DEVICE_ATTR_RO(client_bus_info);
663
664 static struct attribute *dev_attrs[] = {
665                 &dev_attr_partition_handle.attr,
666                 &dev_attr_partition_guid.attr,
667                 &dev_attr_partition_name.attr,
668                 &dev_attr_channel_addr.attr,
669                 &dev_attr_channel_bytes.attr,
670                 &dev_attr_channel_id.attr,
671                 &dev_attr_client_bus_info.attr,
672                 NULL
673 };
674
675 static struct attribute_group dev_attr_grp = {
676                 .attrs = dev_attrs,
677 };
678
679 static const struct attribute_group *visorbus_groups[] = {
680                 &dev_attr_grp,
681                 NULL
682 };
683
684 /*  DRIVER attributes
685  *
686  *  define & implement display of driver attributes under
687  *  /sys/bus/visorbus/drivers/<drivername>.
688  *
689  */
690
691 static ssize_t
692 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
693 {
694         struct visor_driver *drv = to_visor_driver(xdrv);
695
696         return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
697 }
698
699 static int
700 register_driver_attributes(struct visor_driver *drv)
701 {
702         int rc;
703         struct driver_attribute version =
704             __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
705         drv->version_attr = version;
706         rc = driver_create_file(&drv->driver, &drv->version_attr);
707         return rc;
708 }
709
710 static void
711 unregister_driver_attributes(struct visor_driver *drv)
712 {
713         driver_remove_file(&drv->driver, &drv->version_attr);
714 }
715
716 static void
717 dev_periodic_work(void *xdev)
718 {
719         struct visor_device *dev = xdev;
720         struct visor_driver *drv = to_visor_driver(dev->device.driver);
721
722         down(&dev->visordriver_callback_lock);
723         if (drv->channel_interrupt)
724                 drv->channel_interrupt(dev);
725         up(&dev->visordriver_callback_lock);
726         if (!visor_periodic_work_nextperiod(dev->periodic_work))
727                 put_device(&dev->device);
728 }
729
730 static void
731 dev_start_periodic_work(struct visor_device *dev)
732 {
733         if (dev->being_removed)
734                 return;
735         /* now up by at least 2 */
736         get_device(&dev->device);
737         if (!visor_periodic_work_start(dev->periodic_work))
738                 put_device(&dev->device);
739 }
740
741 static void
742 dev_stop_periodic_work(struct visor_device *dev)
743 {
744         if (visor_periodic_work_stop(dev->periodic_work))
745                 put_device(&dev->device);
746 }
747
748 /** This is called automatically upon adding a visor_device (device_add), or
749  *  adding a visor_driver (visorbus_register_visor_driver), but only after
750  *  visorbus_match has returned 1 to indicate a successful match between
751  *  driver and device.
752  */
753 static int
754 visordriver_probe_device(struct device *xdev)
755 {
756         int rc;
757         struct visor_driver *drv;
758         struct visor_device *dev;
759
760         drv = to_visor_driver(xdev->driver);
761         dev = to_visor_device(xdev);
762         down(&dev->visordriver_callback_lock);
763         dev->being_removed = false;
764         /*
765          * ensure that the dev->being_removed flag is cleared before
766          * we start the probe
767          */
768         wmb();
769         get_device(&dev->device);
770         if (!drv->probe) {
771                 up(&dev->visordriver_callback_lock);
772                 rc = -1;
773                 goto away;
774         }
775         rc = drv->probe(dev);
776         if (rc < 0)
777                 goto away;
778
779         fix_vbus_dev_info(dev);
780         up(&dev->visordriver_callback_lock);
781         rc = 0;
782 away:
783         if (rc != 0)
784                 put_device(&dev->device);
785         return rc;
786 }
787
788 /** This is called when device_unregister() is called for each child device
789  *  instance, to notify the appropriate visorbus_driver that the device is
790  *  going away, and to decrease the reference count of the device.
791  */
792 static int
793 visordriver_remove_device(struct device *xdev)
794 {
795         struct visor_device *dev;
796         struct visor_driver *drv;
797
798         dev = to_visor_device(xdev);
799         drv = to_visor_driver(xdev->driver);
800         down(&dev->visordriver_callback_lock);
801         dev->being_removed = true;
802         /*
803          * ensure that the dev->being_removed flag is set before we start the
804          * actual removal
805          */
806         wmb();
807         if (drv) {
808                 if (drv->remove)
809                         drv->remove(dev);
810         }
811         up(&dev->visordriver_callback_lock);
812         dev_stop_periodic_work(dev);
813         devmajorminor_remove_all_files(dev);
814
815         put_device(&dev->device);
816
817         return 0;
818 }
819
820 /** A particular type of visor driver calls this function to register
821  *  the driver.  The caller MUST fill in the following fields within the
822  *  #drv structure:
823  *      name, version, owner, channel_types, probe, remove
824  *
825  *  Here's how the whole Linux bus / driver / device model works.
826  *
827  *  At system start-up, the visorbus kernel module is loaded, which registers
828  *  visorbus_type as a bus type, using bus_register().
829  *
830  *  All kernel modules that support particular device types on a
831  *  visorbus bus are loaded.  Each of these kernel modules calls
832  *  visorbus_register_visor_driver() in their init functions, passing a
833  *  visor_driver struct.  visorbus_register_visor_driver() in turn calls
834  *  register_driver(&visor_driver.driver).  This .driver member is
835  *  initialized with generic methods (like probe), whose sole responsibility
836  *  is to act as a broker for the real methods, which are within the
837  *  visor_driver struct.  (This is the way the subclass behavior is
838  *  implemented, since visor_driver is essentially a subclass of the
839  *  generic driver.)  Whenever a driver_register() happens, core bus code in
840  *  the kernel does (see device_attach() in drivers/base/dd.c):
841  *
842  *      for each dev associated with the bus (the bus that driver is on) that
843  *      does not yet have a driver
844  *          if bus.match(dev,newdriver) == yes_matched  ** .match specified
845  *                                                 ** during bus_register().
846  *              newdriver.probe(dev)  ** for visor drivers, this will call
847  *                    ** the generic driver.probe implemented in visorbus.c,
848  *                    ** which in turn calls the probe specified within the
849  *                    ** struct visor_driver (which was specified by the
850  *                    ** actual device driver as part of
851  *                    ** visorbus_register_visor_driver()).
852  *
853  *  The above dance also happens when a new device appears.
854  *  So the question is, how are devices created within the system?
855  *  Basically, just call device_add(dev).  See pci_bus_add_devices().
856  *  pci_scan_device() shows an example of how to build a device struct.  It
857  *  returns the newly-created struct to pci_scan_single_device(), who adds it
858  *  to the list of devices at PCIBUS.devices.  That list of devices is what
859  *  is traversed by pci_bus_add_devices().
860  *
861  */
862 int visorbus_register_visor_driver(struct visor_driver *drv)
863 {
864         int rc = 0;
865
866         drv->driver.name = drv->name;
867         drv->driver.bus = &visorbus_type;
868         drv->driver.probe = visordriver_probe_device;
869         drv->driver.remove = visordriver_remove_device;
870         drv->driver.owner = drv->owner;
871
872         /* driver_register does this:
873          *   bus_add_driver(drv)
874          *   ->if (drv.bus)  ** (bus_type) **
875          *       driver_attach(drv)
876          *         for each dev with bus type of drv.bus
877          *           if (!dev.drv)  ** no driver assigned yet **
878          *             if (bus.match(dev,drv))  [visorbus_match]
879          *               dev.drv = drv
880          *               if (!drv.probe(dev))   [visordriver_probe_device]
881          *                 dev.drv = NULL
882          */
883
884         rc = driver_register(&drv->driver);
885         if (rc < 0)
886                 return rc;
887         rc = register_driver_attributes(drv);
888         return rc;
889 }
890 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
891
892 /** A particular type of visor driver calls this function to unregister
893  *  the driver, i.e., within its module_exit function.
894  */
895 void
896 visorbus_unregister_visor_driver(struct visor_driver *drv)
897 {
898         unregister_driver_attributes(drv);
899         driver_unregister(&drv->driver);
900 }
901 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
902
903 int
904 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
905                       void *dest, unsigned long nbytes)
906 {
907         return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
908 }
909 EXPORT_SYMBOL_GPL(visorbus_read_channel);
910
911 int
912 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
913                        void *src, unsigned long nbytes)
914 {
915         return visorchannel_write(dev->visorchannel, offset, src, nbytes);
916 }
917 EXPORT_SYMBOL_GPL(visorbus_write_channel);
918
919 int
920 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
921                        unsigned long nbytes)
922 {
923         return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
924 }
925 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
926
927 int
928 visorbus_registerdevnode(struct visor_device *dev,
929                          const char *name, int major, int minor)
930 {
931         return devmajorminor_create_file(dev, name, major, minor);
932 }
933 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
934
935 /** We don't really have a real interrupt, so for now we just call the
936  *  interrupt function periodically...
937  */
938 void
939 visorbus_enable_channel_interrupts(struct visor_device *dev)
940 {
941         dev_start_periodic_work(dev);
942 }
943 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
944
945 void
946 visorbus_disable_channel_interrupts(struct visor_device *dev)
947 {
948         dev_stop_periodic_work(dev);
949 }
950 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
951
952 /** This is how everything starts from the device end.
953  *  This function is called when a channel first appears via a ControlVM
954  *  message.  In response, this function allocates a visor_device to
955  *  correspond to the new channel, and attempts to connect it the appropriate
956  *  driver.  If the appropriate driver is found, the visor_driver.probe()
957  *  function for that driver will be called, and will be passed the new
958  *  visor_device that we just created.
959  *
960  *  It's ok if the appropriate driver is not yet loaded, because in that case
961  *  the new device struct will just stick around in the bus' list of devices.
962  *  When the appropriate driver calls visorbus_register_visor_driver(), the
963  *  visor_driver.probe() for the new driver will be called with the new
964  *  device.
965  */
966 static int
967 create_visor_device(struct visor_device *dev)
968 {
969         int rc = -1;
970         u32 chipset_bus_no = dev->chipset_bus_no;
971         u32 chipset_dev_no = dev->chipset_dev_no;
972
973         POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
974                          POSTCODE_SEVERITY_INFO);
975
976         sema_init(&dev->visordriver_callback_lock, 1);  /* unlocked */
977         dev->device.bus = &visorbus_type;
978         dev->device.groups = visorbus_channel_groups;
979         device_initialize(&dev->device);
980         dev->device.release = visorbus_release_device;
981         /* keep a reference just for us (now 2) */
982         get_device(&dev->device);
983         dev->periodic_work =
984                 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
985                                            periodic_dev_workqueue,
986                                            dev_periodic_work,
987                                            dev, dev_name(&dev->device));
988         if (!dev->periodic_work) {
989                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
990                                  DIAG_SEVERITY_ERR);
991                 goto away;
992         }
993
994         /* bus_id must be a unique name with respect to this bus TYPE
995          * (NOT bus instance).  That's why we need to include the bus
996          * number within the name.
997          */
998         dev_set_name(&dev->device, "vbus%u:dev%u",
999                      chipset_bus_no, chipset_dev_no);
1000
1001         /*  device_add does this:
1002          *    bus_add_device(dev)
1003          *    ->device_attach(dev)
1004          *      ->for each driver drv registered on the bus that dev is on
1005          *          if (dev.drv)  **  device already has a driver **
1006          *            ** not sure we could ever get here... **
1007          *          else
1008          *            if (bus.match(dev,drv)) [visorbus_match]
1009          *              dev.drv = drv
1010          *              if (!drv.probe(dev))  [visordriver_probe_device]
1011          *                dev.drv = NULL
1012          *
1013          *  Note that device_add does NOT fail if no driver failed to
1014          *  claim the device.  The device will be linked onto
1015          *  bus_type.klist_devices regardless (use bus_for_each_dev).
1016          */
1017         rc = device_add(&dev->device);
1018         if (rc < 0) {
1019                 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1020                                  DIAG_SEVERITY_ERR);
1021                 goto away;
1022         }
1023
1024         rc = register_devmajorminor_attributes(dev);
1025         if (rc < 0) {
1026                 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1027                                  DIAG_SEVERITY_ERR);
1028                 goto away_register;
1029         }
1030
1031         list_add_tail(&dev->list_all, &list_all_device_instances);
1032         return 0;
1033
1034 away_register:
1035         device_unregister(&dev->device);
1036 away:
1037         put_device(&dev->device);
1038         return rc;
1039 }
1040
1041 static void
1042 remove_visor_device(struct visor_device *dev)
1043 {
1044         list_del(&dev->list_all);
1045         unregister_devmajorminor_attributes(dev);
1046         put_device(&dev->device);
1047         device_unregister(&dev->device);
1048 }
1049
1050 static int
1051 get_vbus_header_info(struct visorchannel *chan,
1052                      struct spar_vbus_headerinfo *hdr_info)
1053 {
1054         int rc = -1;
1055
1056         if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1057                 goto away;
1058         if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1059                               sizeof(*hdr_info)) < 0) {
1060                 goto away;
1061         }
1062         if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1063                 goto away;
1064         if (hdr_info->device_info_struct_bytes <
1065             sizeof(struct ultra_vbus_deviceinfo)) {
1066                 goto away;
1067         }
1068         rc = 0;
1069 away:
1070         return rc;
1071 }
1072
1073 /* Write the contents of <info> to the struct
1074  * spar_vbus_channel_protocol.chp_info. */
1075
1076 static int
1077 write_vbus_chp_info(struct visorchannel *chan,
1078                     struct spar_vbus_headerinfo *hdr_info,
1079                     struct ultra_vbus_deviceinfo *info)
1080 {
1081         int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1082
1083         if (hdr_info->chp_info_offset == 0)
1084                 return -1;
1085
1086         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1087                 return -1;
1088         return 0;
1089 }
1090
1091 /* Write the contents of <info> to the struct
1092  * spar_vbus_channel_protocol.bus_info. */
1093
1094 static int
1095 write_vbus_bus_info(struct visorchannel *chan,
1096                     struct spar_vbus_headerinfo *hdr_info,
1097                     struct ultra_vbus_deviceinfo *info)
1098 {
1099         int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1100
1101         if (hdr_info->bus_info_offset == 0)
1102                 return -1;
1103
1104         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1105                 return -1;
1106         return 0;
1107 }
1108
1109 /* Write the contents of <info> to the
1110  * struct spar_vbus_channel_protocol.dev_info[<devix>].
1111  */
1112 static int
1113 write_vbus_dev_info(struct visorchannel *chan,
1114                     struct spar_vbus_headerinfo *hdr_info,
1115                     struct ultra_vbus_deviceinfo *info, int devix)
1116 {
1117         int off =
1118             (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1119             (hdr_info->device_info_struct_bytes * devix);
1120
1121         if (hdr_info->dev_info_offset == 0)
1122                 return -1;
1123
1124         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1125                 return -1;
1126         return 0;
1127 }
1128
1129 /* For a child device just created on a client bus, fill in
1130  * information about the driver that is controlling this device into
1131  * the the appropriate slot within the vbus channel of the bus
1132  * instance.
1133  */
1134 static void
1135 fix_vbus_dev_info(struct visor_device *visordev)
1136 {
1137         int i;
1138         struct visor_device *bdev;
1139         struct visor_driver *visordrv;
1140         int bus_no = visordev->chipset_bus_no;
1141         int dev_no = visordev->chipset_dev_no;
1142         struct ultra_vbus_deviceinfo dev_info;
1143         const char *chan_type_name = NULL;
1144         struct spar_vbus_headerinfo *hdr_info;
1145
1146         if (!visordev->device.driver)
1147                 return;
1148
1149         hdr_info = (struct spar_vbus_headerinfo *)visordev->vbus_hdr_info;
1150         if (!hdr_info)
1151                 return;
1152
1153         bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1154         if (!bdev)
1155                 return;
1156
1157         visordrv = to_visor_driver(visordev->device.driver);
1158
1159         /* Within the list of device types (by GUID) that the driver
1160          * says it supports, find out which one of those types matches
1161          * the type of this device, so that we can include the device
1162          * type name
1163          */
1164         for (i = 0; visordrv->channel_types[i].name; i++) {
1165                 if (memcmp(&visordrv->channel_types[i].guid,
1166                            &visordev->channel_type_guid,
1167                            sizeof(visordrv->channel_types[i].guid)) == 0) {
1168                         chan_type_name = visordrv->channel_types[i].name;
1169                         break;
1170                 }
1171         }
1172
1173         bus_device_info_init(&dev_info, chan_type_name,
1174                              visordrv->name, visordrv->version,
1175                              visordrv->vertag);
1176         write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
1177
1178         /* Re-write bus+chipset info, because it is possible that this
1179         * was previously written by our evil counterpart, virtpci.
1180         */
1181         write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
1182         write_vbus_bus_info(bdev->visorchannel, hdr_info,
1183                             &clientbus_driverinfo);
1184 }
1185
1186 /** Create a device instance for the visor bus itself.
1187  */
1188 static int
1189 create_bus_instance(struct visor_device *dev)
1190 {
1191         int rc;
1192         int id = dev->chipset_bus_no;
1193         struct spar_vbus_headerinfo *hdr_info;
1194
1195         POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1196
1197         hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1198         if (!hdr_info) {
1199                 rc = -1;
1200                 goto away;
1201         }
1202
1203         dev_set_name(&dev->device, "visorbus%d", id);
1204         dev->device.bus = &visorbus_type;
1205         dev->device.groups = visorbus_groups;
1206         dev->device.release = visorbus_release_busdevice;
1207
1208         if (device_register(&dev->device) < 0) {
1209                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1210                                  POSTCODE_SEVERITY_ERR);
1211                 rc = -1;
1212                 goto away_mem;
1213         }
1214
1215         if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1216                 dev->vbus_hdr_info = (void *)hdr_info;
1217                 write_vbus_chp_info(dev->visorchannel, hdr_info,
1218                                     &chipset_driverinfo);
1219                 write_vbus_bus_info(dev->visorchannel, hdr_info,
1220                                     &clientbus_driverinfo);
1221         } else {
1222                 kfree(hdr_info);
1223         }
1224         bus_count++;
1225         list_add_tail(&dev->list_all, &list_all_bus_instances);
1226         dev_set_drvdata(&dev->device, dev);
1227         return 0;
1228
1229 away_mem:
1230         kfree(hdr_info);
1231 away:
1232         return rc;
1233 }
1234
1235 /** Remove a device instance for the visor bus itself.
1236  */
1237 static void
1238 remove_bus_instance(struct visor_device *dev)
1239 {
1240         /* Note that this will result in the release method for
1241          * dev->dev being called, which will call
1242          * visorbus_release_busdevice().  This has something to do with
1243          * the put_device() done in device_unregister(), but I have never
1244          * successfully been able to trace thru the code to see where/how
1245          * release() gets called.  But I know it does.
1246          */
1247         bus_count--;
1248         if (dev->visorchannel) {
1249                 visorchannel_destroy(dev->visorchannel);
1250                 dev->visorchannel = NULL;
1251         }
1252         kfree(dev->vbus_hdr_info);
1253         list_del(&dev->list_all);
1254         device_unregister(&dev->device);
1255 }
1256
1257 /** Create and register the one-and-only one instance of
1258  *  the visor bus type (visorbus_type).
1259  */
1260 static int
1261 create_bus_type(void)
1262 {
1263         int rc = 0;
1264
1265         rc = bus_register(&visorbus_type);
1266         return rc;
1267 }
1268
1269 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1270  */
1271 static void
1272 remove_bus_type(void)
1273 {
1274         bus_unregister(&visorbus_type);
1275 }
1276
1277 /** Remove all child visor bus device instances.
1278  */
1279 static void
1280 remove_all_visor_devices(void)
1281 {
1282         struct list_head *listentry, *listtmp;
1283
1284         list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1285                 struct visor_device *dev = list_entry(listentry,
1286                                                       struct visor_device,
1287                                                       list_all);
1288                 remove_visor_device(dev);
1289         }
1290 }
1291
1292 static void
1293 chipset_bus_create(struct visor_device *dev)
1294 {
1295         int rc;
1296         u32 bus_no = dev->chipset_bus_no;
1297
1298         POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1299         rc = create_bus_instance(dev);
1300         POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1301
1302         if (rc < 0)
1303                 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1304                                  POSTCODE_SEVERITY_ERR);
1305         else
1306                 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1307                                  POSTCODE_SEVERITY_INFO);
1308
1309         if (chipset_responders.bus_create)
1310                 (*chipset_responders.bus_create) (dev, rc);
1311 }
1312
1313 static void
1314 chipset_bus_destroy(struct visor_device *dev)
1315 {
1316         remove_bus_instance(dev);
1317         if (chipset_responders.bus_destroy)
1318                 (*chipset_responders.bus_destroy)(dev, 0);
1319 }
1320
1321 static void
1322 chipset_device_create(struct visor_device *dev_info)
1323 {
1324         int rc = -1;
1325         u32 bus_no = dev_info->chipset_bus_no;
1326         u32 dev_no = dev_info->chipset_dev_no;
1327
1328         POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1329                          POSTCODE_SEVERITY_INFO);
1330
1331         rc = create_visor_device(dev_info);
1332         if (chipset_responders.device_create)
1333                 chipset_responders.device_create(dev_info, rc);
1334
1335         if (rc < 0)
1336                 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1337                                  POSTCODE_SEVERITY_ERR);
1338         else
1339                 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1340                                  POSTCODE_SEVERITY_INFO);
1341 }
1342
1343 static void
1344 chipset_device_destroy(struct visor_device *dev_info)
1345 {
1346         remove_visor_device(dev_info);
1347
1348         if (chipset_responders.device_destroy)
1349                 (*chipset_responders.device_destroy) (dev_info, 0);
1350 }
1351
1352 /* This is the callback function specified for a function driver, to
1353  * be called when a pending "pause device" operation has been
1354  * completed.
1355  */
1356 static void
1357 pause_state_change_complete(struct visor_device *dev, int status)
1358 {
1359         if (!dev->pausing)
1360                 return;
1361
1362         dev->pausing = false;
1363         if (!chipset_responders.device_pause) /* this can never happen! */
1364                 return;
1365
1366         /* Notify the chipset driver that the pause is complete, which
1367         * will presumably want to send some sort of response to the
1368         * initiator. */
1369         (*chipset_responders.device_pause) (dev, status);
1370 }
1371
1372 /* This is the callback function specified for a function driver, to
1373  * be called when a pending "resume device" operation has been
1374  * completed.
1375  */
1376 static void
1377 resume_state_change_complete(struct visor_device *dev, int status)
1378 {
1379         if (!dev->resuming)
1380                 return;
1381
1382         dev->resuming = false;
1383         if (!chipset_responders.device_resume) /* this can never happen! */
1384                 return;
1385
1386         /* Notify the chipset driver that the resume is complete,
1387          * which will presumably want to send some sort of response to
1388          * the initiator. */
1389         (*chipset_responders.device_resume) (dev, status);
1390 }
1391
1392 /* Tell the subordinate function driver for a specific device to pause
1393  * or resume that device.  Result is returned asynchronously via a
1394  * callback function.
1395  */
1396 static void
1397 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1398 {
1399         int rc = -1, x;
1400         struct visor_driver *drv = NULL;
1401         void (*notify_func)(struct visor_device *dev, int response) = NULL;
1402
1403         if (is_pause)
1404                 notify_func = chipset_responders.device_pause;
1405         else
1406                 notify_func = chipset_responders.device_resume;
1407         if (!notify_func)
1408                 goto away;
1409
1410         drv = to_visor_driver(dev->device.driver);
1411         if (!drv)
1412                 goto away;
1413
1414         if (dev->pausing || dev->resuming)
1415                 goto away;
1416
1417         /* Note that even though both drv->pause() and drv->resume
1418          * specify a callback function, it is NOT necessary for us to
1419          * increment our local module usage count.  Reason is, there
1420          * is already a linkage dependency between child function
1421          * drivers and visorbus, so it is already IMPOSSIBLE to unload
1422          * visorbus while child function drivers are still running.
1423          */
1424         if (is_pause) {
1425                 if (!drv->pause)
1426                         goto away;
1427
1428                 dev->pausing = true;
1429                 x = drv->pause(dev, pause_state_change_complete);
1430         } else {
1431                 /* This should be done at BUS resume time, but an
1432                  * existing problem prevents us from ever getting a bus
1433                  * resume...  This hack would fail to work should we
1434                  * ever have a bus that contains NO devices, since we
1435                  * would never even get here in that case. */
1436                 fix_vbus_dev_info(dev);
1437                 if (!drv->resume)
1438                         goto away;
1439
1440                 dev->resuming = true;
1441                 x = drv->resume(dev, resume_state_change_complete);
1442         }
1443         if (x < 0) {
1444                 if (is_pause)
1445                         dev->pausing = false;
1446                 else
1447                         dev->resuming = false;
1448                 goto away;
1449         }
1450         rc = 0;
1451 away:
1452         if (rc < 0) {
1453                 if (notify_func)
1454                         (*notify_func)(dev, rc);
1455         }
1456 }
1457
1458 static void
1459 chipset_device_pause(struct visor_device *dev_info)
1460 {
1461         initiate_chipset_device_pause_resume(dev_info, true);
1462 }
1463
1464 static void
1465 chipset_device_resume(struct visor_device *dev_info)
1466 {
1467         initiate_chipset_device_pause_resume(dev_info, false);
1468 }
1469
1470 struct channel_size_info {
1471         uuid_le guid;
1472         unsigned long min_size;
1473         unsigned long max_size;
1474 };
1475
1476 int
1477 visorbus_init(void)
1478 {
1479         int rc = 0;
1480
1481         POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1482         bus_device_info_init(&clientbus_driverinfo,
1483                              "clientbus", "visorbus",
1484                              VERSION, NULL);
1485
1486         rc = create_bus_type();
1487         if (rc < 0) {
1488                 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1489                 goto away;
1490         }
1491
1492         periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
1493         if (!periodic_dev_workqueue) {
1494                 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
1495                 rc = -ENOMEM;
1496                 goto away;
1497         }
1498
1499         /* This enables us to receive notifications when devices appear for
1500          * which this service partition is to be a server for.
1501          */
1502         visorchipset_register_busdev(&chipset_notifiers,
1503                                      &chipset_responders,
1504                                      &chipset_driverinfo);
1505
1506         rc = 0;
1507
1508 away:
1509         if (rc)
1510                 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
1511                                  POSTCODE_SEVERITY_ERR);
1512         return rc;
1513 }
1514
1515 void
1516 visorbus_exit(void)
1517 {
1518         struct list_head *listentry, *listtmp;
1519
1520         visorchipset_register_busdev(NULL, NULL, NULL);
1521         remove_all_visor_devices();
1522
1523         flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
1524         destroy_workqueue(periodic_dev_workqueue);
1525         periodic_dev_workqueue = NULL;
1526
1527         if (periodic_test_workqueue) {
1528                 cancel_delayed_work(&periodic_work);
1529                 flush_workqueue(periodic_test_workqueue);
1530                 destroy_workqueue(periodic_test_workqueue);
1531                 periodic_test_workqueue = NULL;
1532         }
1533
1534         list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1535                 struct visor_device *dev = list_entry(listentry,
1536                                                       struct visor_device,
1537                                                       list_all);
1538                 remove_bus_instance(dev);
1539         }
1540         remove_bus_type();
1541 }
1542
1543 module_param_named(debug, visorbus_debug, int, S_IRUGO);
1544 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
1545
1546 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1547 MODULE_PARM_DESC(visorbus_forcematch,
1548                  "1 to force a successful dev <--> drv match");
1549
1550 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1551 MODULE_PARM_DESC(visorbus_forcenomatch,
1552                  "1 to force an UNsuccessful dev <--> drv match");
1553
1554 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
1555 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");