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