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