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