staging: unisys: visorchannel: Make visorchannel_create take a gfp_t
[linux-2.6-block.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 "vbusdeviceinfo.h"
27
28 #define MYDRVNAME "visorbus"
29
30 /* module parameters */
31 int visorbus_debug;
32 int visorbus_forcematch;
33 int visorbus_forcenomatch;
34 #define MAXDEVICETEST 4
35 int visorbus_devicetest;
36 int visorbus_debugref;
37 #define SERIALLOOPBACKCHANADDR (100 * 1024 * 1024)
38
39 /** This is the private data that we store for each bus device instance.
40  */
41 struct visorbus_devdata {
42         int devno;              /* this is the chipset busNo */
43         struct list_head list_all;
44         struct device *dev;
45         struct kobject kobj;
46         struct visorchannel *chan;      /* channel area for bus itself */
47         bool vbus_valid;
48         struct spar_vbus_headerinfo vbus_hdr_info;
49 };
50
51 /* These forward declarations are required since our drivers are out-of-tree.
52  * The structures referenced are kernel-private and are not in the headers, but
53  * it is impossible to make a functioning bus driver without them.
54  */
55 struct subsys_private {
56         struct kset subsys;
57         struct kset *devices_kset;
58
59         struct kset *drivers_kset;
60         struct klist klist_devices;
61         struct klist klist_drivers;
62         struct blocking_notifier_head bus_notifier;
63         unsigned int drivers_autoprobe:1;
64         struct bus_type *bus;
65
66         struct list_head class_interfaces;
67         struct kset glue_dirs;
68         struct mutex class_mutex; /* ignore */
69         struct class *class;
70 };
71
72 struct bus_type_private {
73         struct kset subsys;
74         struct kset *drivers_kset;
75         struct kset *devices_kset;
76         struct klist klist_devices;
77         struct klist klist_drivers;
78         struct blocking_notifier_head bus_notifier;
79         unsigned int drivers_autoprobe:1;
80         struct bus_type *bus;
81 };
82
83 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
84 #define POLLJIFFIES_TESTWORK         100
85 #define POLLJIFFIES_NORMALCHANNEL     10
86
87 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env);
88 static int visorbus_match(struct device *xdev, struct device_driver *xdrv);
89 static void fix_vbus_dev_info(struct visor_device *visordev);
90
91 /** This describes the TYPE of bus.
92  *  (Don't confuse this with an INSTANCE of the bus.)
93  */
94 static struct bus_type visorbus_type = {
95         .name = "visorbus",
96         .match = visorbus_match,
97         .uevent = visorbus_uevent,
98 };
99
100 static struct delayed_work periodic_work;
101
102 /* YES, we need 2 workqueues.
103  * The reason is, workitems on the test queue may need to cancel
104  * workitems on the other queue.  You will be in for trouble if you try to
105  * do this with workitems queued on the same workqueue.
106  */
107 static struct workqueue_struct *periodic_test_workqueue;
108 static struct workqueue_struct *periodic_dev_workqueue;
109 static long long bus_count;     /** number of bus instances */
110 static long long total_devices_created;
111                                         /** ever-increasing */
112
113 static void chipset_bus_create(u32 bus_no);
114 static void chipset_bus_destroy(u32 bus_no);
115 static void chipset_device_create(u32 bus_no, u32 dev_no);
116 static void chipset_device_destroy(u32 bus_no, u32 dev_no);
117 static void chipset_device_pause(u32 bus_no, u32 dev_no);
118 static void chipset_device_resume(u32 bus_no, u32 dev_no);
119
120 /** These functions are implemented herein, and are called by the chipset
121  *  driver to notify us about specific events.
122  */
123 static struct visorchipset_busdev_notifiers chipset_notifiers = {
124         .bus_create = chipset_bus_create,
125         .bus_destroy = chipset_bus_destroy,
126         .device_create = chipset_device_create,
127         .device_destroy = chipset_device_destroy,
128         .device_pause = chipset_device_pause,
129         .device_resume = chipset_device_resume,
130 };
131
132 /** These functions are implemented in the chipset driver, and we call them
133  *  herein when we want to acknowledge a specific event.
134  */
135 static struct visorchipset_busdev_responders chipset_responders;
136
137 /* filled in with info about parent chipset driver when we register with it */
138 static struct ultra_vbus_deviceinfo chipset_driverinfo;
139 /* filled in with info about this driver, wrt it servicing client busses */
140 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
141
142 /** list of visorbus_devdata structs, linked via .list_all */
143 static LIST_HEAD(list_all_bus_instances);
144 /** list of visor_device structs, linked via .list_all */
145 static LIST_HEAD(list_all_device_instances);
146
147 static int
148 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
149 {
150         if (add_uevent_var(env, "VERSION=%s", VERSION))
151                 return -ENOMEM;
152         return 0;
153 }
154
155 /* This is called automatically upon adding a visor_device (device_add), or
156  * adding a visor_driver (visorbus_register_visor_driver), and returns 1 iff the
157  * provided driver can control the specified device.
158  */
159 static int
160 visorbus_match(struct device *xdev, struct device_driver *xdrv)
161 {
162         uuid_le channel_type;
163         int rc = 0;
164         int i;
165         struct visor_device *dev;
166         struct visor_driver *drv;
167
168         dev = to_visor_device(xdev);
169         drv = to_visor_driver(xdrv);
170         channel_type = visorchannel_get_uuid(dev->visorchannel);
171         if (visorbus_forcematch) {
172                 rc = 1;
173                 goto away;
174         }
175         if (visorbus_forcenomatch)
176                 goto away;
177
178         if (!drv->channel_types)
179                 goto away;
180         for (i = 0;
181              (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
182              (drv->channel_types[i].name);
183              i++)
184                 if (uuid_le_cmp(drv->channel_types[i].guid,
185                                 channel_type) == 0) {
186                         rc = i + 1;
187                         goto away;
188                 }
189 away:
190         return rc;
191 }
192
193 /** This is called when device_unregister() is called for the bus device
194  *  instance, after all other tasks involved with destroying the device
195  *  are complete.
196  */
197 static void
198 visorbus_release_busdevice(struct device *xdev)
199 {
200         struct visorbus_devdata *devdata = dev_get_drvdata(xdev);
201
202         dev_set_drvdata(xdev, NULL);
203         kfree(devdata);
204         kfree(xdev);
205 }
206
207 /** This is called when device_unregister() is called for each child
208  *  device instance.
209  */
210 static void
211 visorbus_release_device(struct device *xdev)
212 {
213         struct visor_device *dev = to_visor_device(xdev);
214
215         if (dev->periodic_work) {
216                 visor_periodic_work_destroy(dev->periodic_work);
217                 dev->periodic_work = NULL;
218         }
219         if (dev->visorchannel) {
220                 visorchannel_destroy(dev->visorchannel);
221                 dev->visorchannel = NULL;
222         }
223         kfree(dev);
224 }
225
226 /* Implement publishing of device node attributes under:
227  *
228  *     /sys/bus/visorbus<x>/dev<y>/devmajorminor
229  *
230  */
231
232 #define to_devmajorminor_attr(_attr) \
233         container_of(_attr, struct devmajorminor_attribute, attr)
234 #define to_visor_device_from_kobjdevmajorminor(obj) \
235         container_of(obj, struct visor_device, kobjdevmajorminor)
236
237 struct devmajorminor_attribute {
238         struct attribute attr;
239         int slot;
240          ssize_t (*show)(struct visor_device *, int slot, char *buf);
241          ssize_t (*store)(struct visor_device *, int slot, const char *buf,
242                           size_t count);
243 };
244
245 static ssize_t DEVMAJORMINOR_ATTR(struct visor_device *dev, int slot, char *buf)
246 {
247         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
248
249         if (slot < 0 || slot >= maxdevnodes)
250                 return 0;
251         return snprintf(buf, PAGE_SIZE, "%d:%d\n",
252                         dev->devnodes[slot].major, dev->devnodes[slot].minor);
253 }
254
255 static ssize_t
256 devmajorminor_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
257 {
258         struct devmajorminor_attribute *devmajorminor_attr =
259             to_devmajorminor_attr(attr);
260         struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
261         ssize_t ret = 0;
262
263         if (devmajorminor_attr->show)
264                 ret = devmajorminor_attr->show(dev,
265                                                devmajorminor_attr->slot, buf);
266         return ret;
267 }
268
269 static ssize_t
270 devmajorminor_attr_store(struct kobject *kobj,
271                          struct attribute *attr, const char *buf, size_t count)
272 {
273         struct devmajorminor_attribute *devmajorminor_attr =
274             to_devmajorminor_attr(attr);
275         struct visor_device *dev = to_visor_device_from_kobjdevmajorminor(kobj);
276         ssize_t ret = 0;
277
278         if (devmajorminor_attr->store)
279                 ret = devmajorminor_attr->store(dev,
280                                                 devmajorminor_attr->slot,
281                                                 buf, count);
282         return ret;
283 }
284
285 static int register_devmajorminor_attributes(struct visor_device *dev);
286
287 int
288 devmajorminor_create_file(struct visor_device *dev, const char *name,
289                           int major, int minor)
290 {
291         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
292         struct devmajorminor_attribute *myattr = NULL;
293         int x = -1, rc = 0, slot = -1;
294
295         register_devmajorminor_attributes(dev);
296         for (slot = 0; slot < maxdevnodes; slot++)
297                 if (!dev->devnodes[slot].attr)
298                         break;
299         if (slot == maxdevnodes) {
300                 rc = -ENOMEM;
301                 goto away;
302         }
303         myattr = kmalloc(sizeof(*myattr), GFP_KERNEL);
304         if (!myattr) {
305                 rc = -ENOMEM;
306                 goto away;
307         }
308         memset(myattr, 0, sizeof(struct devmajorminor_attribute));
309         myattr->show = DEVMAJORMINOR_ATTR;
310         myattr->store = NULL;
311         myattr->slot = slot;
312         myattr->attr.name = name;
313         myattr->attr.mode = S_IRUGO;
314         dev->devnodes[slot].attr = myattr;
315         dev->devnodes[slot].major = major;
316         dev->devnodes[slot].minor = minor;
317         x = sysfs_create_file(&dev->kobjdevmajorminor, &myattr->attr);
318         if (x < 0) {
319                 rc = x;
320                 goto away;
321         }
322         kobject_uevent(&dev->device.kobj, KOBJ_ONLINE);
323 away:
324         if (rc < 0) {
325                 kfree(myattr);
326                 myattr = NULL;
327                 dev->devnodes[slot].attr = NULL;
328         }
329         return rc;
330 }
331
332 void
333 devmajorminor_remove_file(struct visor_device *dev, int slot)
334 {
335         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
336         struct devmajorminor_attribute *myattr = NULL;
337
338         if (slot < 0 || slot >= maxdevnodes)
339                 return;
340         myattr = (struct devmajorminor_attribute *)(dev->devnodes[slot].attr);
341         if (myattr)
342                 return;
343         sysfs_remove_file(&dev->kobjdevmajorminor, &myattr->attr);
344         kobject_uevent(&dev->device.kobj, KOBJ_OFFLINE);
345         dev->devnodes[slot].attr = NULL;
346         kfree(myattr);
347 }
348
349 void
350 devmajorminor_remove_all_files(struct visor_device *dev)
351 {
352         int i = 0;
353         int maxdevnodes = ARRAY_SIZE(dev->devnodes) / sizeof(dev->devnodes[0]);
354
355         for (i = 0; i < maxdevnodes; i++)
356                 devmajorminor_remove_file(dev, i);
357 }
358
359 static const struct sysfs_ops devmajorminor_sysfs_ops = {
360         .show = devmajorminor_attr_show,
361         .store = devmajorminor_attr_store,
362 };
363
364 static struct kobj_type devmajorminor_kobj_type = {
365         .sysfs_ops = &devmajorminor_sysfs_ops
366 };
367
368 static int
369 register_devmajorminor_attributes(struct visor_device *dev)
370 {
371         int rc = 0, x = 0;
372
373         if (dev->kobjdevmajorminor.parent)
374                 goto away;      /* already registered */
375         x = kobject_init_and_add(&dev->kobjdevmajorminor,
376                                  &devmajorminor_kobj_type, &dev->device.kobj,
377                                  "devmajorminor");
378         if (x < 0) {
379                 rc = x;
380                 goto away;
381         }
382
383         kobject_uevent(&dev->kobjdevmajorminor, KOBJ_ADD);
384
385 away:
386         return rc;
387 }
388
389 void
390 unregister_devmajorminor_attributes(struct visor_device *dev)
391 {
392         if (!dev->kobjdevmajorminor.parent)
393                 return;         /* already unregistered */
394         devmajorminor_remove_all_files(dev);
395
396         kobject_del(&dev->kobjdevmajorminor);
397         kobject_put(&dev->kobjdevmajorminor);
398         dev->kobjdevmajorminor.parent = NULL;
399 }
400
401 /* Implement publishing of channel attributes under:
402  *
403  *     /sys/bus/visorbus<x>/dev<y>/channel
404  *
405  */
406
407 #define to_channel_attr(_attr) \
408         container_of(_attr, struct channel_attribute, attr)
409 #define to_visor_device_from_kobjchannel(obj) \
410         container_of(obj, struct visor_device, kobjchannel)
411
412 struct channel_attribute {
413         struct attribute attr;
414          ssize_t (*show)(struct visor_device*, char *buf);
415          ssize_t (*store)(struct visor_device*, const char *buf, size_t count);
416 };
417
418 /* begin implementation of specific channel attributes to appear under
419 * /sys/bus/visorbus<x>/dev<y>/channel
420 */
421 static ssize_t devicechannel_attr_physaddr(struct visor_device *dev, char *buf)
422 {
423         if (!dev->visorchannel)
424                 return 0;
425         return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
426                         visorchannel_get_physaddr(dev->visorchannel));
427 }
428
429 static ssize_t devicechannel_attr_nbytes(struct visor_device *dev, char *buf)
430 {
431         if (!dev->visorchannel)
432                 return 0;
433         return snprintf(buf, PAGE_SIZE, "0x%lx\n",
434                         visorchannel_get_nbytes(dev->visorchannel));
435 }
436
437 static ssize_t devicechannel_attr_clientpartition(struct visor_device *dev,
438                                                   char *buf) {
439         if (!dev->visorchannel)
440                 return 0;
441         return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
442                         visorchannel_get_clientpartition(dev->visorchannel));
443 }
444
445 static ssize_t devicechannel_attr_typeguid(struct visor_device *dev, char *buf)
446 {
447         char s[99];
448
449         if (!dev->visorchannel)
450                 return 0;
451         return snprintf(buf, PAGE_SIZE, "%s\n",
452                         visorchannel_id(dev->visorchannel, s));
453 }
454
455 static ssize_t devicechannel_attr_zoneguid(struct visor_device *dev, char *buf)
456 {
457         char s[99];
458
459         if (!dev->visorchannel)
460                 return 0;
461         return snprintf(buf, PAGE_SIZE, "%s\n",
462                         visorchannel_zoneid(dev->visorchannel, s));
463 }
464
465 static ssize_t devicechannel_attr_typename(struct visor_device *dev, char *buf)
466 {
467         int i = 0;
468         struct bus_type *xbus = dev->device.bus;
469         struct device_driver *xdrv = dev->device.driver;
470         struct visor_driver *drv = NULL;
471
472         if (!dev->visorchannel || !xbus || !xdrv)
473                 return 0;
474         i = xbus->match(&dev->device, xdrv);
475         if (!i)
476                 return 0;
477         drv = to_visor_driver(xdrv);
478         return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
479 }
480
481 static ssize_t devicechannel_attr_dump(struct visor_device *dev, char *buf)
482 {
483         int count = 0;
484 /* TODO: replace this with debugfs code
485         struct seq_file *m = NULL;
486         if (dev->visorchannel == NULL)
487                 return 0;
488         m = visor_seq_file_new_buffer(buf, PAGE_SIZE - 1);
489         if (m == NULL)
490                 return 0;
491         visorchannel_debug(dev->visorchannel, 1, m, 0);
492         count = m->count;
493         visor_seq_file_done_buffer(m);
494         m = NULL;
495 */
496         return count;
497 }
498
499 static struct channel_attribute all_channel_attrs[] = {
500         __ATTR(physaddr, S_IRUGO,
501                devicechannel_attr_physaddr, NULL),
502         __ATTR(nbytes, S_IRUGO,
503                devicechannel_attr_nbytes, NULL),
504         __ATTR(clientpartition, S_IRUGO,
505                devicechannel_attr_clientpartition, NULL),
506         __ATTR(typeguid, S_IRUGO,
507                devicechannel_attr_typeguid, NULL),
508         __ATTR(zoneguid, S_IRUGO,
509                devicechannel_attr_zoneguid, NULL),
510         __ATTR(typename, S_IRUGO,
511                devicechannel_attr_typename, NULL),
512         __ATTR(dump, S_IRUGO,
513                devicechannel_attr_dump, NULL),
514 };
515
516 /* end implementation of specific channel attributes */
517
518 static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
519                                  char *buf)
520 {
521         struct channel_attribute *channel_attr = to_channel_attr(attr);
522         struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
523         ssize_t ret = 0;
524
525         if (channel_attr->show)
526                 ret = channel_attr->show(dev, buf);
527         return ret;
528 }
529
530 static ssize_t channel_attr_store(struct kobject *kobj, struct attribute *attr,
531                                   const char *buf, size_t count)
532 {
533         struct channel_attribute *channel_attr = to_channel_attr(attr);
534         struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
535         ssize_t ret = 0;
536
537         if (channel_attr->store)
538                 ret = channel_attr->store(dev, buf, count);
539         return ret;
540 }
541
542 static int channel_create_file(struct visor_device *dev,
543                                struct channel_attribute *attr)
544 {
545         return sysfs_create_file(&dev->kobjchannel, &attr->attr);
546 }
547
548 static void channel_remove_file(struct visor_device *dev,
549                                 struct channel_attribute *attr)
550 {
551         sysfs_remove_file(&dev->kobjchannel, &attr->attr);
552 }
553
554 static const struct sysfs_ops channel_sysfs_ops = {
555         .show = channel_attr_show,
556         .store = channel_attr_store,
557 };
558
559 static struct kobj_type channel_kobj_type = {
560         .sysfs_ops = &channel_sysfs_ops
561 };
562
563 int register_channel_attributes(struct visor_device *dev)
564 {
565         int rc = 0, i = 0, x = 0;
566
567         if (dev->kobjchannel.parent)
568                 goto away;      /* already registered */
569         x = kobject_init_and_add(&dev->kobjchannel, &channel_kobj_type,
570                                  &dev->device.kobj, "channel");
571         if (x < 0) {
572                 rc = x;
573                 goto away;
574         }
575
576         kobject_uevent(&dev->kobjchannel, KOBJ_ADD);
577
578         for (i = 0;
579              i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
580              i++)
581                 x = channel_create_file(dev, &all_channel_attrs[i]);
582         if (x < 0) {
583                 while (--i >= 0)
584                         channel_remove_file(dev, &all_channel_attrs[i]);
585                 kobject_del(&dev->kobjchannel);
586                 kobject_put(&dev->kobjchannel);
587                 rc = x;
588                 goto away;
589         }
590 away:
591         return rc;
592 }
593
594 void unregister_channel_attributes(struct visor_device *dev)
595 {
596         int i = 0;
597
598         if (!dev->kobjchannel.parent)
599                 return;         /* already unregistered */
600         for (i = 0;
601              i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
602              i++)
603                 channel_remove_file(dev, &all_channel_attrs[i]);
604
605         kobject_del(&dev->kobjchannel);
606         kobject_put(&dev->kobjchannel);
607         dev->kobjchannel.parent = NULL;
608 }
609 /*   This is actually something they forgot to put in the kernel.
610  *   struct bus_type in the kernel SHOULD have a "busses" member, which
611  *   should be treated similarly to the "devices" and "drivers" members.
612  *   There SHOULD be:
613  *   - a "businst_attribute" analogous to the existing "bus_attribute"
614  *   - a "businst_create_file" and "businst_remove_file" analogous to the
615  *     existing "bus_create_file" and "bus_remove_file".
616  *   That's what I created businst.c and businst.h to do.
617  *
618  *   We want to add the "busses" sub-tree in sysfs, where we will house the
619  *   names and properties of each bus instance:
620  *
621  *       /sys/bus/<bustypename>/
622  *           version
623  *           devices
624  *               <devname1> --> /sys/devices/<businstancename><devname1>
625  *               <devname2> --> /sys/devices/<businstancename><devname2>
626  *           drivers
627  *               <driverinstancename1>
628  *                   <driverinstance1property1>
629  *                   <driverinstance1property2>
630  *                   ...
631  *               <driverinstancename2>
632  *                   <driverinstance2property1>
633  *                   <driverinstance2property2>
634  *                   ...
635  *   >>      busses
636  *   >>          <businstancename1>
637  *   >>              <businstance1property1>
638  *   >>              <businstance1property2>
639  *   >>              ...
640  *   >>          <businstancename2>
641  *   >>              <businstance2property1>
642  *   >>              <businstance2property2>
643  *   >>              ...
644  *
645  *   I considered adding bus instance properties under
646  *   /sys/devices/<businstancename>.  But I thought there may be existing
647  *   notions that ONLY device sub-trees should live under
648  *   /sys/devices/<businstancename>.  So I stayed out of there.
649  *
650  */
651
652 struct businst_attribute {
653         struct attribute attr;
654          ssize_t (*show)(struct visorbus_devdata*, char *buf);
655          ssize_t (*store)(struct visorbus_devdata*, const char *buf,
656                           size_t count);
657 };
658
659 #define to_businst_attr(_attr) \
660         container_of(_attr, struct businst_attribute, attr)
661 #define to_visorbus_devdata(obj) \
662         container_of(obj, struct visorbus_devdata, kobj)
663
664 static ssize_t
665 businst_attr_show(struct kobject *kobj, struct attribute *attr,
666                   char *buf)
667 {
668         struct businst_attribute *businst_attr = to_businst_attr(attr);
669         struct visorbus_devdata *bus = to_visorbus_devdata(kobj);
670         ssize_t ret = 0;
671
672         if (businst_attr->show)
673                 ret = businst_attr->show(bus, buf);
674         return ret;
675 }
676
677 static ssize_t
678 businst_attr_store(struct kobject *kobj, struct attribute *attr,
679                    const char *buf, size_t count)
680 {
681         struct businst_attribute *businst_attr = to_businst_attr(attr);
682         struct visorbus_devdata *bus = to_visorbus_devdata(kobj);
683         ssize_t ret = 0;
684
685         if (businst_attr->store)
686                 ret = businst_attr->store(bus, buf, count);
687         return ret;
688 }
689
690 static int
691 businst_create_file(struct visorbus_devdata *bus,
692                     struct businst_attribute *attr)
693 {
694         return sysfs_create_file(&bus->kobj, &attr->attr);
695 }
696
697 static void
698 businst_remove_file(struct visorbus_devdata *bus,
699                     struct businst_attribute *attr)
700 {
701         sysfs_remove_file(&bus->kobj, &attr->attr);
702 }
703
704 static const struct sysfs_ops businst_sysfs_ops = {
705         .show = businst_attr_show,
706         .store = businst_attr_store,
707 };
708
709 static struct kobj_type businst_kobj_type = {
710         .sysfs_ops = &businst_sysfs_ops
711 };
712
713 static struct kset businstances = { /* should actually be a member of
714                                      * bus_type */
715 };
716
717 /*  BUS type attributes
718  *
719  *  define & implement display of bus attributes under
720  *  /sys/bus/visorbus.
721  *
722  */
723
724 static ssize_t
725 BUSTYPE_ATTR_version(struct bus_type *bus, char *buf)
726 {
727         return snprintf(buf, PAGE_SIZE, "%s\n", VERSION);
728 }
729
730 static struct bus_attribute bustype_attr_version =
731 __ATTR(version, S_IRUGO, BUSTYPE_ATTR_version, NULL);
732
733 static int
734 register_bustype_attributes(void)
735 {
736         int rc = 0;
737
738         rc = bus_create_file(&visorbus_type, &bustype_attr_version);
739         if (rc < 0)
740                         goto away;
741
742         /* Here we make up for the fact that bus_type does not yet have a
743          * member to keep track of multiple bus instances for a given bus
744          * type.  This is useful for stashing properties for each bus
745          * instance.
746          */
747         kobject_set_name(&businstances.kobj, "busses");
748         businstances.kobj.ktype = &businst_kobj_type;
749         businstances.kobj.parent = &visorbus_type.p->subsys.kobj;
750         rc = kset_register(&businstances);
751         if (rc < 0)
752                         goto away;
753
754         rc = 0;
755 away:
756         return rc;
757 }
758
759 static void
760 unregister_bustype_attributes(void)
761 {
762         bus_remove_file(&visorbus_type, &bustype_attr_version);
763         kset_unregister(&businstances);
764 }
765
766 /*  BUS instance attributes
767  *
768  *  define & implement display of bus attributes under
769  *  /sys/bus/visorbus/busses/visorbus<n>.
770  *
771  *  This is a bit hoaky because the kernel does not yet have the infrastructure
772  *  to separate bus INSTANCE attributes from bus TYPE attributes...
773  *  so we roll our own.  See businst.c / businst.h.
774  *
775  */
776
777 static ssize_t businst_attr_partition_handle(struct visorbus_devdata *businst,
778                                              char *buf) {
779         struct visorchipset_bus_info bus_info;
780         int len = 0;
781
782         if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
783                 len = snprintf(buf, PAGE_SIZE,
784                                "0x%Lx\n",
785                                (unsigned long long)bus_info.partition_handle);
786         return len;
787 }
788
789 static ssize_t businst_attr_partition_guid(struct visorbus_devdata *businst,
790                                            char *buf) {
791         struct visorchipset_bus_info bus_info;
792         int len = 0;
793
794         if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
795                 len = snprintf(buf, PAGE_SIZE, "{%pUb}\n",
796                                &bus_info.partition_uuid);
797         return len;
798 }
799
800 static ssize_t businst_attr_partition_name(struct visorbus_devdata *businst,
801                                            char *buf) {
802         struct visorchipset_bus_info bus_info;
803         int len = 0;
804
805         if (businst &&
806             visorchipset_get_bus_info(businst->devno, &bus_info) &&
807             bus_info.name)
808                 len = snprintf(buf, PAGE_SIZE, "%s\n", bus_info.name);
809         return len;
810 }
811
812 static ssize_t businst_attr_channel_addr(struct visorbus_devdata *businst,
813                                          char *buf) {
814         struct visorchipset_bus_info bus_info;
815         int len = 0;
816
817         if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
818                 len = snprintf(buf, PAGE_SIZE, "0x%Lx\n", (unsigned long long)
819                                bus_info.chan_info.channel_addr);
820         return len;
821 }
822
823 static ssize_t businst_attr_nchannel_bytes(struct visorbus_devdata *businst,
824                                            char *buf) {
825         struct visorchipset_bus_info bus_info;
826         int len = 0;
827
828         if (businst && visorchipset_get_bus_info(businst->devno, &bus_info))
829                 len = snprintf(buf, PAGE_SIZE, "0x%Lx\n", (unsigned long long)
830                                bus_info.chan_info.n_channel_bytes);
831         return len;
832 }
833
834 static ssize_t businst_attr_channel_id(struct visorbus_devdata *businst,
835                                        char *buf) {
836         int len = 0;
837
838         if (businst && businst->chan) {
839                 visorchannel_id(businst->chan, buf);
840                 len = strlen(buf);
841                 buf[len++] = '\n';
842         }
843         return len;
844 }
845
846 static ssize_t businst_attr_client_bus_info(struct visorbus_devdata *businst,
847                                             char *buf) {
848         struct visorchipset_bus_info bus_info;
849         int i, x, remain = PAGE_SIZE;
850         unsigned long off;
851         char *p = buf;
852         u8 *partition_name;
853         struct ultra_vbus_deviceinfo dev_info;
854
855         partition_name = "";
856         if (businst && businst->chan) {
857                 if (visorchipset_get_bus_info(businst->devno, &bus_info) &&
858                     bus_info.name)
859                         partition_name = bus_info.name;
860                 x = snprintf(p, remain,
861                              "Client device / client driver info for %s partition (vbus #%d):\n",
862                              partition_name, businst->devno);
863                 p += x;
864                 remain -= x;
865                 x = visorchannel_read(businst->chan,
866                                       offsetof(struct
867                                                spar_vbus_channel_protocol,
868                                                chp_info),
869                                       &dev_info, sizeof(dev_info));
870                 if (x >= 0) {
871                         x = vbuschannel_devinfo_to_string(&dev_info, p,
872                                                           remain, -1);
873                         p += x;
874                         remain -= x;
875                 }
876                 x = visorchannel_read(businst->chan,
877                                       offsetof(struct
878                                                spar_vbus_channel_protocol,
879                                                bus_info),
880                                       &dev_info, sizeof(dev_info));
881                 if (x >= 0) {
882                         x = vbuschannel_devinfo_to_string(&dev_info, p,
883                                                           remain, -1);
884                         p += x;
885                         remain -= x;
886                 }
887                 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
888                 i = 0;
889                 while (off + sizeof(dev_info) <=
890                        visorchannel_get_nbytes(businst->chan)) {
891                         x = visorchannel_read(businst->chan,
892                                               off, &dev_info, sizeof(dev_info));
893                         if (x >= 0) {
894                                 x = vbuschannel_devinfo_to_string
895                                     (&dev_info, p, remain, i);
896                                 p += x;
897                                 remain -= x;
898                         }
899                         off += sizeof(dev_info);
900                         i++;
901                 }
902         }
903         return PAGE_SIZE - remain;
904 }
905
906 static struct businst_attribute ba_partition_handle =
907         __ATTR(partition_handle, S_IRUGO, businst_attr_partition_handle, NULL);
908 static struct businst_attribute ba_partition_guid =
909         __ATTR(partition_guid, S_IRUGO, businst_attr_partition_guid, NULL);
910 static struct businst_attribute ba_partition_name =
911         __ATTR(partition_name, S_IRUGO, businst_attr_partition_name, NULL);
912 static struct businst_attribute ba_channel_addr =
913         __ATTR(channel_addr, S_IRUGO, businst_attr_channel_addr, NULL);
914 static struct businst_attribute ba_nchannel_bytes =
915         __ATTR(nchannel_bytes, S_IRUGO, businst_attr_nchannel_bytes, NULL);
916 static struct businst_attribute ba_channel_id =
917         __ATTR(channel_id, S_IRUGO, businst_attr_channel_id, NULL);
918 static struct businst_attribute ba_client_bus_info =
919         __ATTR(client_bus_info, S_IRUGO, businst_attr_client_bus_info, NULL);
920
921 static int
922 register_businst_attributes(struct visorbus_devdata *businst)
923 {
924         int rc = 0;
925
926         businst->kobj.kset = &businstances;     /* identify parent sysfs dir */
927         rc = kobject_init_and_add(&businst->kobj, &businst_kobj_type,
928                                   NULL, "visorbus%d", businst->devno);
929         if (rc < 0)
930                         goto away;
931
932         rc = businst_create_file(businst, &ba_partition_handle);
933         if (rc < 0)
934                         goto away;
935
936         rc = businst_create_file(businst, &ba_partition_guid);
937         if (rc < 0)
938                         goto away;
939
940         rc = businst_create_file(businst, &ba_partition_name);
941         if (rc < 0)
942                         goto away;
943
944         rc = businst_create_file(businst, &ba_channel_addr);
945         if (rc < 0)
946                         goto away;
947
948         rc = businst_create_file(businst, &ba_nchannel_bytes);
949         if (rc < 0)
950                         goto away;
951
952         rc = businst_create_file(businst, &ba_channel_id);
953         if (rc < 0)
954                         goto away;
955
956         rc = businst_create_file(businst, &ba_client_bus_info);
957         if (rc < 0)
958                         goto away;
959
960         kobject_uevent(&businst->kobj, KOBJ_ADD);
961
962         rc = 0;
963 away:
964         return rc;
965 }
966
967 static void
968 unregister_businst_attributes(struct visorbus_devdata *businst)
969 {
970         businst_remove_file(businst, &ba_partition_handle);
971         businst_remove_file(businst, &ba_partition_guid);
972         businst_remove_file(businst, &ba_partition_name);
973         businst_remove_file(businst, &ba_channel_addr);
974         businst_remove_file(businst, &ba_nchannel_bytes);
975         businst_remove_file(businst, &ba_channel_id);
976         businst_remove_file(businst, &ba_client_bus_info);
977         kobject_put(&businst->kobj);
978 }
979
980 /*  DRIVER attributes
981  *
982  *  define & implement display of driver attributes under
983  *  /sys/bus/visorbus/drivers/<drivername>.
984  *
985  */
986
987 static ssize_t
988 DRIVER_ATTR_version(struct device_driver *xdrv, char *buf)
989 {
990         struct visor_driver *drv = to_visor_driver(xdrv);
991
992         return snprintf(buf, PAGE_SIZE, "%s\n", drv->version);
993 }
994
995 static int
996 register_driver_attributes(struct visor_driver *drv)
997 {
998         int rc;
999         struct driver_attribute version =
1000             __ATTR(version, S_IRUGO, DRIVER_ATTR_version, NULL);
1001         drv->version_attr = version;
1002         rc = driver_create_file(&drv->driver, &drv->version_attr);
1003         return rc;
1004 }
1005
1006 static void
1007 unregister_driver_attributes(struct visor_driver *drv)
1008 {
1009         driver_remove_file(&drv->driver, &drv->version_attr);
1010 }
1011
1012 /*  DEVICE attributes
1013  *
1014  *  define & implement display of device attributes under
1015  *  /sys/bus/visorbus/devices/<devicename>.
1016  *
1017  */
1018
1019 #define DEVATTR(nam, func) { \
1020         .attr = { .name = __stringify(nam), \
1021                   .mode = 0444, \
1022                   .owner = THIS_MODULE },       \
1023         .show = func, \
1024 }
1025
1026 static struct device_attribute visor_device_attrs[] = {
1027         /* DEVATTR(channel_nbytes, DEVICE_ATTR_channel_nbytes), */
1028         __ATTR_NULL
1029 };
1030
1031 static void
1032 dev_periodic_work(void *xdev)
1033 {
1034         struct visor_device *dev = (struct visor_device *)xdev;
1035         struct visor_driver *drv = to_visor_driver(dev->device.driver);
1036
1037         down(&dev->visordriver_callback_lock);
1038         if (drv->channel_interrupt)
1039                 drv->channel_interrupt(dev);
1040         up(&dev->visordriver_callback_lock);
1041         if (!visor_periodic_work_nextperiod(dev->periodic_work))
1042                 put_device(&dev->device);
1043 }
1044
1045 static void
1046 dev_start_periodic_work(struct visor_device *dev)
1047 {
1048         if (dev->being_removed)
1049                 return;
1050         /* now up by at least 2 */
1051         get_device(&dev->device);
1052         if (!visor_periodic_work_start(dev->periodic_work))
1053                 put_device(&dev->device);
1054 }
1055
1056 static void
1057 dev_stop_periodic_work(struct visor_device *dev)
1058 {
1059         if (visor_periodic_work_stop(dev->periodic_work))
1060                 put_device(&dev->device);
1061 }
1062
1063 /** This is called automatically upon adding a visor_device (device_add), or
1064  *  adding a visor_driver (visorbus_register_visor_driver), but only after
1065  *  visorbus_match has returned 1 to indicate a successful match between
1066  *  driver and device.
1067  */
1068 static int
1069 visordriver_probe_device(struct device *xdev)
1070 {
1071         int rc;
1072         struct visor_driver *drv;
1073         struct visor_device *dev;
1074
1075         drv = to_visor_driver(xdev->driver);
1076         dev = to_visor_device(xdev);
1077         down(&dev->visordriver_callback_lock);
1078         dev->being_removed = false;
1079         /*
1080          * ensure that the dev->being_removed flag is cleared before
1081          * we start the probe
1082          */
1083         wmb();
1084         get_device(&dev->device);
1085         if (!drv->probe) {
1086                 up(&dev->visordriver_callback_lock);
1087                 rc = -1;
1088                 goto away;
1089         }
1090         rc = drv->probe(dev);
1091         if (rc < 0)
1092                 goto away;
1093
1094         fix_vbus_dev_info(dev);
1095         up(&dev->visordriver_callback_lock);
1096         rc = 0;
1097 away:
1098         if (rc != 0)
1099                 put_device(&dev->device);
1100         /*  We could get here more than once if the child driver module is
1101          *  unloaded and re-loaded while devices are present.  That's why we
1102          *  need a flag to be sure that we only respond to the device_create
1103          *  once.  We cannot respond to the device_create prior to here,
1104          *  because until we call drv->probe() above, the channel has not been
1105          *  initialized.
1106          */
1107         if (!dev->responded_to_device_create) {
1108                 dev->responded_to_device_create = true;
1109                 if (chipset_responders.device_create)
1110                         (*chipset_responders.device_create)(dev->chipset_bus_no,
1111                                                             dev->chipset_dev_no,
1112                                                             rc);
1113         }
1114         return rc;
1115 }
1116
1117 /** This is called when device_unregister() is called for each child device
1118  *  instance, to notify the appropriate visorbus_driver that the device is
1119  *  going away, and to decrease the reference count of the device.
1120  */
1121 static int
1122 visordriver_remove_device(struct device *xdev)
1123 {
1124         int rc = 0;
1125         struct visor_device *dev;
1126         struct visor_driver *drv;
1127
1128         dev = to_visor_device(xdev);
1129         drv = to_visor_driver(xdev->driver);
1130         down(&dev->visordriver_callback_lock);
1131         dev->being_removed = true;
1132         /*
1133          * ensure that the dev->being_removed flag is set before we start the
1134          * actual removal
1135          */
1136         wmb();
1137         if (drv) {
1138                 if (drv->remove)
1139                         drv->remove(dev);
1140         }
1141         up(&dev->visordriver_callback_lock);
1142         dev_stop_periodic_work(dev);
1143         devmajorminor_remove_all_files(dev);
1144
1145         put_device(&dev->device);
1146
1147         return rc;
1148 }
1149
1150 /** A particular type of visor driver calls this function to register
1151  *  the driver.  The caller MUST fill in the following fields within the
1152  *  #drv structure:
1153  *      name, version, owner, channel_types, probe, remove
1154  *
1155  *  Here's how the whole Linux bus / driver / device model works.
1156  *
1157  *  At system start-up, the visorbus kernel module is loaded, which registers
1158  *  visorbus_type as a bus type, using bus_register().
1159  *
1160  *  All kernel modules that support particular device types on a
1161  *  visorbus bus are loaded.  Each of these kernel modules calls
1162  *  visorbus_register_visor_driver() in their init functions, passing a
1163  *  visor_driver struct.  visorbus_register_visor_driver() in turn calls
1164  *  register_driver(&visor_driver.driver).  This .driver member is
1165  *  initialized with generic methods (like probe), whose sole responsibility
1166  *  is to act as a broker for the real methods, which are within the
1167  *  visor_driver struct.  (This is the way the subclass behavior is
1168  *  implemented, since visor_driver is essentially a subclass of the
1169  *  generic driver.)  Whenever a driver_register() happens, core bus code in
1170  *  the kernel does (see device_attach() in drivers/base/dd.c):
1171  *
1172  *      for each dev associated with the bus (the bus that driver is on) that
1173  *      does not yet have a driver
1174  *          if bus.match(dev,newdriver) == yes_matched  ** .match specified
1175  *                                                 ** during bus_register().
1176  *              newdriver.probe(dev)  ** for visor drivers, this will call
1177  *                    ** the generic driver.probe implemented in visorbus.c,
1178  *                    ** which in turn calls the probe specified within the
1179  *                    ** struct visor_driver (which was specified by the
1180  *                    ** actual device driver as part of
1181  *                    ** visorbus_register_visor_driver()).
1182  *
1183  *  The above dance also happens when a new device appears.
1184  *  So the question is, how are devices created within the system?
1185  *  Basically, just call device_add(dev).  See pci_bus_add_devices().
1186  *  pci_scan_device() shows an example of how to build a device struct.  It
1187  *  returns the newly-created struct to pci_scan_single_device(), who adds it
1188  *  to the list of devices at PCIBUS.devices.  That list of devices is what
1189  *  is traversed by pci_bus_add_devices().
1190  *
1191  */
1192 int visorbus_register_visor_driver(struct visor_driver *drv)
1193 {
1194         int rc = 0;
1195
1196         drv->driver.name = drv->name;
1197         drv->driver.bus = &visorbus_type;
1198         drv->driver.probe = visordriver_probe_device;
1199         drv->driver.remove = visordriver_remove_device;
1200         drv->driver.owner = drv->owner;
1201
1202         /* driver_register does this:
1203          *   bus_add_driver(drv)
1204          *   ->if (drv.bus)  ** (bus_type) **
1205          *       driver_attach(drv)
1206          *         for each dev with bus type of drv.bus
1207          *           if (!dev.drv)  ** no driver assigned yet **
1208          *             if (bus.match(dev,drv))  [visorbus_match]
1209          *               dev.drv = drv
1210          *               if (!drv.probe(dev))   [visordriver_probe_device]
1211          *                 dev.drv = NULL
1212          */
1213
1214         rc = driver_register(&drv->driver);
1215         if (rc < 0)
1216                 return rc;
1217         rc = register_driver_attributes(drv);
1218         return rc;
1219 }
1220 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1221
1222 /** A particular type of visor driver calls this function to unregister
1223  *  the driver, i.e., within its module_exit function.
1224  */
1225 void
1226 visorbus_unregister_visor_driver(struct visor_driver *drv)
1227 {
1228         unregister_driver_attributes(drv);
1229         driver_unregister(&drv->driver);
1230 }
1231 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
1232
1233 int
1234 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
1235                       void *dest, unsigned long nbytes)
1236 {
1237         return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
1238 }
1239 EXPORT_SYMBOL_GPL(visorbus_read_channel);
1240
1241 int
1242 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
1243                        void *src, unsigned long nbytes)
1244 {
1245         return visorchannel_write(dev->visorchannel, offset, src, nbytes);
1246 }
1247 EXPORT_SYMBOL_GPL(visorbus_write_channel);
1248
1249 int
1250 visorbus_clear_channel(struct visor_device *dev, unsigned long offset, u8 ch,
1251                        unsigned long nbytes)
1252 {
1253         return visorchannel_clear(dev->visorchannel, offset, ch, nbytes);
1254 }
1255 EXPORT_SYMBOL_GPL(visorbus_clear_channel);
1256
1257 int
1258 visorbus_registerdevnode(struct visor_device *dev,
1259                          const char *name, int major, int minor)
1260 {
1261         return devmajorminor_create_file(dev, name, major, minor);
1262 }
1263 EXPORT_SYMBOL_GPL(visorbus_registerdevnode);
1264
1265 /** We don't really have a real interrupt, so for now we just call the
1266  *  interrupt function periodically...
1267  */
1268 void
1269 visorbus_enable_channel_interrupts(struct visor_device *dev)
1270 {
1271         dev_start_periodic_work(dev);
1272 }
1273 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
1274
1275 void
1276 visorbus_disable_channel_interrupts(struct visor_device *dev)
1277 {
1278         dev_stop_periodic_work(dev);
1279 }
1280 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
1281
1282 /** This is how everything starts from the device end.
1283  *  This function is called when a channel first appears via a ControlVM
1284  *  message.  In response, this function allocates a visor_device to
1285  *  correspond to the new channel, and attempts to connect it the appropriate
1286  *  driver.  If the appropriate driver is found, the visor_driver.probe()
1287  *  function for that driver will be called, and will be passed the new
1288  *  visor_device that we just created.
1289  *
1290  *  It's ok if the appropriate driver is not yet loaded, because in that case
1291  *  the new device struct will just stick around in the bus' list of devices.
1292  *  When the appropriate driver calls visorbus_register_visor_driver(), the
1293  *  visor_driver.probe() for the new driver will be called with the new
1294  *  device.
1295  */
1296 static int
1297 create_visor_device(struct visorbus_devdata *devdata,
1298                     unsigned long chipset_bus_no, unsigned long chipset_dev_no,
1299                     struct visorchipset_channel_info chan_info,
1300                     u64 partition_handle)
1301 {
1302         int rc = -1;
1303         struct visorchannel *visorchannel = NULL;
1304         struct visor_device *dev = NULL;
1305         bool gotten = false, registered1 = false, registered2 = false;
1306
1307         POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
1308                          POSTCODE_SEVERITY_INFO);
1309         /* prepare chan_hdr (abstraction to read/write channel memory) */
1310         visorchannel = visorchannel_create(chan_info.channel_addr,
1311                                            chan_info.n_channel_bytes,
1312                                            GFP_KERNEL,
1313                                            chan_info.channel_type_uuid);
1314         if (!visorchannel) {
1315                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1316                                  DIAG_SEVERITY_ERR);
1317                 goto away;
1318         }
1319         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1320         if (!dev) {
1321                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1322                                  DIAG_SEVERITY_ERR);
1323                 goto away;
1324         }
1325
1326         memset(dev, 0, sizeof(struct visor_device));
1327         dev->visorchannel = visorchannel;
1328         dev->channel_type_guid = chan_info.channel_type_uuid;
1329         dev->channel_bytes = chan_info.n_channel_bytes;
1330         dev->chipset_bus_no = chipset_bus_no;
1331         dev->chipset_dev_no = chipset_dev_no;
1332         dev->device.parent = devdata->dev;
1333         sema_init(&dev->visordriver_callback_lock, 1);  /* unlocked */
1334         dev->device.bus = &visorbus_type;
1335         device_initialize(&dev->device);
1336         dev->device.release = visorbus_release_device;
1337         /* keep a reference just for us (now 2) */
1338         get_device(&dev->device);
1339         gotten = true;
1340         dev->periodic_work =
1341                 visor_periodic_work_create(POLLJIFFIES_NORMALCHANNEL,
1342                                            periodic_dev_workqueue,
1343                                            dev_periodic_work,
1344                                            dev, dev_name(&dev->device));
1345         if (!dev->periodic_work) {
1346                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, chipset_dev_no,
1347                                  DIAG_SEVERITY_ERR);
1348                 goto away;
1349         }
1350
1351         /* bus_id must be a unique name with respect to this bus TYPE
1352          * (NOT bus instance).  That's why we need to include the bus
1353          * number within the name.
1354          */
1355         dev_set_name(&dev->device, "vbus%lu:dev%lu",
1356                      chipset_bus_no, chipset_dev_no);
1357
1358         /*  device_add does this:
1359          *    bus_add_device(dev)
1360          *    ->device_attach(dev)
1361          *      ->for each driver drv registered on the bus that dev is on
1362          *          if (dev.drv)  **  device already has a driver **
1363          *            ** not sure we could ever get here... **
1364          *          else
1365          *            if (bus.match(dev,drv)) [visorbus_match]
1366          *              dev.drv = drv
1367          *              if (!drv.probe(dev))  [visordriver_probe_device]
1368          *                dev.drv = NULL
1369          *
1370          *  Note that device_add does NOT fail if no driver failed to
1371          *  claim the device.  The device will be linked onto
1372          *  bus_type.klist_devices regardless (use bus_for_each_dev).
1373          */
1374         rc = device_add(&dev->device);
1375         if (rc < 0) {
1376                 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
1377                                  DIAG_SEVERITY_ERR);
1378                 goto away;
1379         }
1380
1381         /* note: device_register is simply device_initialize + device_add */
1382         rc = register_channel_attributes(dev);
1383         if (rc < 0) {
1384                 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1385                                  DIAG_SEVERITY_ERR);
1386                 goto away;
1387         }
1388
1389         registered1 = true;
1390
1391         rc = register_devmajorminor_attributes(dev);
1392         if (rc < 0) {
1393                 POSTCODE_LINUX_3(DEVICE_REGISTER_FAILURE_PC, chipset_dev_no,
1394                                  DIAG_SEVERITY_ERR);
1395                 goto away;
1396         }
1397
1398         registered2 = true;
1399         rc = 0;
1400
1401 away:
1402         if (rc < 0) {
1403                 if (registered2)
1404                         unregister_devmajorminor_attributes(dev);
1405                 if (registered1)
1406                         unregister_channel_attributes(dev);
1407                 if (gotten)
1408                         put_device(&dev->device);
1409                 if (visorchannel)
1410                         visorchannel_destroy(visorchannel);
1411                 kfree(dev);
1412         } else {
1413                 total_devices_created++;
1414                 list_add_tail(&dev->list_all, &list_all_device_instances);
1415         }
1416         return rc;
1417 }
1418
1419 static void
1420 remove_visor_device(struct visor_device *dev)
1421 {
1422         list_del(&dev->list_all);
1423         unregister_devmajorminor_attributes(dev);
1424         unregister_channel_attributes(dev);
1425         put_device(&dev->device);
1426         device_unregister(&dev->device);
1427 }
1428
1429 static struct visor_device *
1430 find_visor_device_by_channel(u64 channel_physaddr)
1431 {
1432         struct list_head *listentry, *listtmp;
1433
1434         list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1435                 struct visor_device *dev = list_entry(listentry,
1436                                                       struct visor_device,
1437                                                       list_all);
1438                 if (visorchannel_get_physaddr(dev->visorchannel) ==
1439                     channel_physaddr)
1440                         return dev;
1441         }
1442         return NULL;
1443 }
1444
1445 static int
1446 init_vbus_channel(struct visorchannel *chan)
1447 {
1448         int rc = -1;
1449         unsigned long allocated_bytes = visorchannel_get_nbytes(chan);
1450         struct spar_vbus_channel_protocol *x =
1451                 kmalloc(sizeof(struct spar_vbus_channel_protocol),
1452                         GFP_KERNEL);
1453
1454         POSTCODE_LINUX_3(VBUS_CHANNEL_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
1455
1456         if (x) {
1457                 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1458                 goto away;
1459         }
1460         if (visorchannel_clear(chan, 0, 0, allocated_bytes) < 0) {
1461                 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1462                                  POSTCODE_SEVERITY_ERR);
1463                 goto away;
1464         }
1465         if (visorchannel_read
1466             (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1467                 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1468                                  POSTCODE_SEVERITY_ERR);
1469                 goto away;
1470         }
1471         if (!SPAR_VBUS_CHANNEL_OK_SERVER(allocated_bytes)) {
1472                 POSTCODE_LINUX_2(VBUS_CHANNEL_FAILURE_PC,
1473                                  POSTCODE_SEVERITY_ERR);
1474                 goto away;
1475         }
1476
1477         if (visorchannel_write
1478             (chan, 0, x, sizeof(struct spar_vbus_channel_protocol)) < 0) {
1479                 POSTCODE_LINUX_3(VBUS_CHANNEL_FAILURE_PC, chan,
1480                                  POSTCODE_SEVERITY_ERR);
1481                 goto away;
1482         }
1483
1484         POSTCODE_LINUX_3(VBUS_CHANNEL_EXIT_PC, chan, POSTCODE_SEVERITY_INFO);
1485         rc = 0;
1486
1487 away:
1488         kfree(x);
1489         x = NULL;
1490         return rc;
1491 }
1492
1493 static int
1494 get_vbus_header_info(struct visorchannel *chan,
1495                      struct spar_vbus_headerinfo *hdr_info)
1496 {
1497         int rc = -1;
1498
1499         if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
1500                 goto away;
1501         if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
1502                               sizeof(*hdr_info)) < 0) {
1503                 goto away;
1504         }
1505         if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
1506                 goto away;
1507         if (hdr_info->device_info_struct_bytes <
1508             sizeof(struct ultra_vbus_deviceinfo)) {
1509                 goto away;
1510         }
1511         rc = 0;
1512 away:
1513         return rc;
1514 }
1515
1516 /* Write the contents of <info> to the struct
1517  * spar_vbus_channel_protocol.chp_info. */
1518
1519 static int
1520 write_vbus_chp_info(struct visorchannel *chan,
1521                     struct spar_vbus_headerinfo *hdr_info,
1522                     struct ultra_vbus_deviceinfo *info)
1523 {
1524         int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
1525
1526         if (hdr_info->chp_info_offset == 0)
1527                         return -1;
1528
1529         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1530                         return -1;
1531         return 0;
1532 }
1533
1534 /* Write the contents of <info> to the struct
1535  * spar_vbus_channel_protocol.bus_info. */
1536
1537 static int
1538 write_vbus_bus_info(struct visorchannel *chan,
1539                     struct spar_vbus_headerinfo *hdr_info,
1540                     struct ultra_vbus_deviceinfo *info)
1541 {
1542         int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
1543
1544         if (hdr_info->bus_info_offset == 0)
1545                         return -1;
1546
1547         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1548                         return -1;
1549         return 0;
1550 }
1551
1552 /* Write the contents of <info> to the
1553  * struct spar_vbus_channel_protocol.dev_info[<devix>].
1554  */
1555 static int
1556 write_vbus_dev_info(struct visorchannel *chan,
1557                     struct spar_vbus_headerinfo *hdr_info,
1558                     struct ultra_vbus_deviceinfo *info, int devix)
1559 {
1560         int off =
1561             (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
1562             (hdr_info->device_info_struct_bytes * devix);
1563
1564         if (hdr_info->dev_info_offset == 0)
1565                         return -1;
1566
1567         if (visorchannel_write(chan, off, info, sizeof(*info)) < 0)
1568                         return -1;
1569         return 0;
1570 }
1571
1572 /* For a child device just created on a client bus, fill in
1573  * information about the driver that is controlling this device into
1574  * the the appropriate slot within the vbus channel of the bus
1575  * instance.
1576  */
1577 static void
1578 fix_vbus_dev_info(struct visor_device *visordev)
1579 {
1580         int i;
1581         struct visorchipset_bus_info bus_info;
1582         struct visorbus_devdata *devdata = NULL;
1583         struct visor_driver *visordrv;
1584         int bus_no = visordev->chipset_bus_no;
1585         int dev_no = visordev->chipset_dev_no;
1586         struct ultra_vbus_deviceinfo dev_info;
1587         const char *chan_type_name = NULL;
1588
1589         if (!visordev->device.driver)
1590                         return;
1591
1592         visordrv = to_visor_driver(visordev->device.driver);
1593         if (!visorchipset_get_bus_info(bus_no, &bus_info))
1594                         return;
1595
1596         devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1597         if (!devdata)
1598                         return;
1599
1600         if (!devdata->vbus_valid)
1601                         return;
1602
1603         /* Within the list of device types (by GUID) that the driver
1604          * says it supports, find out which one of those types matches
1605          * the type of this device, so that we can include the device
1606          * type name
1607          */
1608         for (i = 0; visordrv->channel_types[i].name; i++) {
1609                 if (memcmp(&visordrv->channel_types[i].guid,
1610                            &visordev->channel_type_guid,
1611                            sizeof(visordrv->channel_types[i].guid)) == 0) {
1612                         chan_type_name = visordrv->channel_types[i].name;
1613                         break;
1614                 }
1615         }
1616
1617         bus_device_info_init(&dev_info, chan_type_name,
1618                              visordrv->name, visordrv->version,
1619                              visordrv->vertag);
1620         write_vbus_dev_info(devdata->chan,
1621                             &devdata->vbus_hdr_info, &dev_info, dev_no);
1622
1623         /* Re-write bus+chipset info, because it is possible that this
1624         * was previously written by our evil counterpart, virtpci.
1625         */
1626         write_vbus_chp_info(devdata->chan, &devdata->vbus_hdr_info,
1627                             &chipset_driverinfo);
1628         write_vbus_bus_info(devdata->chan, &devdata->vbus_hdr_info,
1629                             &clientbus_driverinfo);
1630 }
1631
1632 /** Create a device instance for the visor bus itself.
1633  */
1634 static struct visorbus_devdata *
1635 create_bus_instance(int id)
1636 {
1637         struct visorbus_devdata *rc = NULL;
1638         struct visorbus_devdata *devdata = NULL;
1639         struct device *dev;
1640         struct visorchipset_bus_info bus_info;
1641
1642         POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1643         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1644         if (!dev) {
1645                 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1646                 rc = NULL;
1647                 goto away;
1648         }
1649         memset(dev, 0, sizeof(struct device));
1650         dev_set_name(dev, "visorbus%d", id);
1651         dev->release = visorbus_release_busdevice;
1652         if (device_register(dev) < 0) {
1653                 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
1654                                  POSTCODE_SEVERITY_ERR);
1655                 rc = NULL;
1656                 goto away;
1657         }
1658         devdata = kmalloc(sizeof(*devdata), GFP_KERNEL);
1659         if (!devdata) {
1660                 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1661                 rc = NULL;
1662                 goto away;
1663         }
1664         memset(devdata, 0, sizeof(struct visorbus_devdata));
1665         devdata->devno = id;
1666         devdata->dev = dev;
1667         if ((visorchipset_get_bus_info(id, &bus_info)) &&
1668             (bus_info.chan_info.channel_addr > 0) &&
1669             (bus_info.chan_info.n_channel_bytes > 0)) {
1670                 u64 channel_addr = bus_info.chan_info.channel_addr;
1671                 unsigned long n_channel_bytes =
1672                                 (unsigned long)
1673                                 bus_info.chan_info.n_channel_bytes;
1674                 uuid_le channel_type_guid =
1675                                 bus_info.chan_info.channel_type_uuid;
1676
1677                 devdata->chan = visorchannel_create(channel_addr,
1678                                                     n_channel_bytes,
1679                                                     GFP_KERNEL,
1680                                                     channel_type_guid);
1681                 if (!devdata->chan) {
1682                         POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, channel_addr,
1683                                          POSTCODE_SEVERITY_ERR);
1684                 } else {
1685                         if (bus_info.flags.server) {
1686                                 init_vbus_channel(devdata->chan);
1687                         } else {
1688                                 if (get_vbus_header_info(devdata->chan,
1689                                                          &devdata->
1690                                                          vbus_hdr_info) >= 0) {
1691                                         devdata->vbus_valid = true;
1692                                         write_vbus_chp_info(devdata->chan,
1693                                                             &devdata->
1694                                                             vbus_hdr_info,
1695                                                             &chipset_driverinfo
1696                                                             );
1697                                         write_vbus_bus_info(devdata->chan,
1698                                                             &devdata->
1699                                                                 vbus_hdr_info,
1700                                                         &clientbus_driverinfo);
1701                                 }
1702                         }
1703                 }
1704         }
1705         register_businst_attributes(devdata);
1706         bus_count++;
1707         list_add_tail(&devdata->list_all, &list_all_bus_instances);
1708         if (id == 0)
1709                         devdata = devdata;      /* for testing ONLY */
1710         dev_set_drvdata(dev, devdata);
1711         rc = devdata;
1712 away:
1713         return rc;
1714 }
1715
1716 /** Remove a device instance for the visor bus itself.
1717  */
1718 static void
1719 remove_bus_instance(struct visorbus_devdata *devdata)
1720 {
1721         /* Note that this will result in the release method for
1722          * devdata->dev being called, which will call
1723          * visorbus_release_busdevice().  This has something to do with
1724          * the put_device() done in device_unregister(), but I have never
1725          * successfully been able to trace thru the code to see where/how
1726          * release() gets called.  But I know it does.
1727          */
1728         unregister_businst_attributes(devdata);
1729         bus_count--;
1730         if (devdata->chan) {
1731                 visorchannel_destroy(devdata->chan);
1732                 devdata->chan = NULL;
1733         }
1734         list_del(&devdata->list_all);
1735         device_unregister(devdata->dev);
1736 }
1737
1738 /** Create and register the one-and-only one instance of
1739  *  the visor bus type (visorbus_type).
1740  */
1741 static int
1742 create_bus_type(void)
1743 {
1744         int rc = 0;
1745
1746         visorbus_type.dev_attrs = visor_device_attrs;
1747         rc = bus_register(&visorbus_type);
1748         if (rc < 0)
1749                         return rc;
1750
1751         rc = register_bustype_attributes();
1752         return rc;
1753 }
1754
1755 /** Remove the one-and-only one instance of the visor bus type (visorbus_type).
1756  */
1757 static void
1758 remove_bus_type(void)
1759 {
1760         unregister_bustype_attributes();
1761         bus_unregister(&visorbus_type);
1762 }
1763
1764 /** Remove all child visor bus device instances.
1765  */
1766 static void
1767 remove_all_visor_devices(void)
1768 {
1769         struct list_head *listentry, *listtmp;
1770
1771         list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1772                 struct visor_device *dev = list_entry(listentry,
1773                                                       struct visor_device,
1774                                                       list_all);
1775                 remove_visor_device(dev);
1776         }
1777 }
1778
1779 static bool entered_testing_mode;
1780 static struct visorchipset_channel_info test_channel_infos[MAXDEVICETEST];
1781 static unsigned long test_bus_nos[MAXDEVICETEST];
1782 static unsigned long test_dev_nos[MAXDEVICETEST];
1783
1784 static void
1785 chipset_bus_create(u32 bus_no)
1786 {
1787         struct visorchipset_bus_info bus_info;
1788         struct visorbus_devdata *devdata;
1789         int rc = -1;
1790
1791         POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1792         if (!visorchipset_get_bus_info(bus_no, &bus_info))
1793                 goto away;
1794         devdata = create_bus_instance(bus_no);
1795         if (!devdata)
1796                 goto away;
1797         if (!visorchipset_set_bus_context(bus_no, devdata))
1798                 goto away;
1799         POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1800         rc = 0;
1801 away:
1802         if (rc < 0) {
1803                 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1804                                  POSTCODE_SEVERITY_ERR);
1805                 return;
1806         }
1807         POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1808                          POSTCODE_SEVERITY_INFO);
1809         if (chipset_responders.bus_create)
1810                 (*chipset_responders.bus_create) (bus_no, rc);
1811 }
1812
1813 static void
1814 chipset_bus_destroy(u32 bus_no)
1815 {
1816         struct visorchipset_bus_info bus_info;
1817         struct visorbus_devdata *devdata;
1818         int rc = -1;
1819
1820         if (!visorchipset_get_bus_info(bus_no, &bus_info))
1821                 goto away;
1822         devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1823         if (!devdata)
1824                 goto away;
1825         remove_bus_instance(devdata);
1826         if (!visorchipset_set_bus_context(bus_no, NULL))
1827                 goto away;
1828         rc = 0;
1829 away:
1830         if (rc < 0)
1831                 return;
1832         if (chipset_responders.bus_destroy)
1833                 (*chipset_responders.bus_destroy)(bus_no, rc);
1834 }
1835
1836 static void
1837 chipset_device_create(u32 bus_no, u32 dev_no)
1838 {
1839         struct visorchipset_device_info dev_info;
1840         struct visorchipset_bus_info bus_info;
1841         struct visorbus_devdata *devdata = NULL;
1842         int rc = -1;
1843
1844         POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1845                          POSTCODE_SEVERITY_INFO);
1846
1847         if (entered_testing_mode)
1848                 return;
1849         if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1850                 goto away;
1851         if (!visorchipset_get_bus_info(bus_no, &bus_info))
1852                 goto away;
1853         if (visorbus_devicetest)
1854                 if (total_devices_created < MAXDEVICETEST) {
1855                         test_channel_infos[total_devices_created] =
1856                             dev_info.chan_info;
1857                         test_bus_nos[total_devices_created] = bus_no;
1858                         test_dev_nos[total_devices_created] = dev_no;
1859                 }
1860         POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
1861                          POSTCODE_SEVERITY_INFO);
1862         rc = 0;
1863 away:
1864         if (rc < 0) {
1865                 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1866                                  POSTCODE_SEVERITY_ERR);
1867                 return;
1868         }
1869         devdata = (struct visorbus_devdata *)(bus_info.bus_driver_context);
1870         rc = create_visor_device(devdata, bus_no, dev_no,
1871                                  dev_info.chan_info, bus_info.partition_handle);
1872         POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1873                          POSTCODE_SEVERITY_INFO);
1874         if (rc < 0)
1875                 if (chipset_responders.device_create)
1876                         (*chipset_responders.device_create)(bus_no, dev_no, rc);
1877 }
1878
1879 static void
1880 chipset_device_destroy(u32 bus_no, u32 dev_no)
1881 {
1882         struct visorchipset_device_info dev_info;
1883         struct visor_device *dev;
1884         int rc = -1;
1885
1886         if (entered_testing_mode)
1887                 return;
1888         if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1889                 goto away;
1890         dev = find_visor_device_by_channel(dev_info.chan_info.channel_addr);
1891         if (!dev)
1892                 goto away;
1893         rc = 0;
1894 away:
1895         if (rc < 0)
1896                         return;
1897
1898         if (chipset_responders.device_destroy)
1899                 (*chipset_responders.device_destroy) (bus_no, dev_no, rc);
1900         remove_visor_device(dev);
1901 }
1902
1903 /* This is the callback function specified for a function driver, to
1904  * be called when a pending "pause device" operation has been
1905  * completed.
1906  */
1907 static void
1908 pause_state_change_complete(struct visor_device *dev, int status)
1909 {
1910         if (!dev->pausing)
1911                         return;
1912
1913         dev->pausing = false;
1914         if (!chipset_responders.device_pause) /* this can never happen! */
1915                         return;
1916
1917         /* Notify the chipset driver that the pause is complete, which
1918         * will presumably want to send some sort of response to the
1919         * initiator. */
1920         (*chipset_responders.device_pause) (dev->chipset_bus_no,
1921                                             dev->chipset_dev_no, status);
1922 }
1923
1924 /* This is the callback function specified for a function driver, to
1925  * be called when a pending "resume device" operation has been
1926  * completed.
1927  */
1928 static void
1929 resume_state_change_complete(struct visor_device *dev, int status)
1930 {
1931         if (!dev->resuming)
1932                         return;
1933
1934         dev->resuming = false;
1935         if (!chipset_responders.device_resume) /* this can never happen! */
1936                         return;
1937
1938         /* Notify the chipset driver that the resume is complete,
1939          * which will presumably want to send some sort of response to
1940          * the initiator. */
1941         (*chipset_responders.device_resume) (dev->chipset_bus_no,
1942                                              dev->chipset_dev_no, status);
1943 }
1944
1945 /* Tell the subordinate function driver for a specific device to pause
1946  * or resume that device.  Result is returned asynchronously via a
1947  * callback function.
1948  */
1949 static void
1950 initiate_chipset_device_pause_resume(u32 bus_no, u32 dev_no, bool is_pause)
1951 {
1952         struct visorchipset_device_info dev_info;
1953         struct visor_device *dev = NULL;
1954         int rc = -1, x;
1955         struct visor_driver *drv = NULL;
1956         void (*notify_func)(u32 bus_no, u32 dev_no, int response) = NULL;
1957
1958         if (is_pause)
1959                 notify_func = chipset_responders.device_pause;
1960         else
1961                 notify_func = chipset_responders.device_resume;
1962         if (!notify_func)
1963                         goto away;
1964
1965         if (!visorchipset_get_device_info(bus_no, dev_no, &dev_info))
1966                         goto away;
1967
1968         dev = find_visor_device_by_channel(dev_info.chan_info.channel_addr);
1969         if (!dev)
1970                         goto away;
1971
1972         drv = to_visor_driver(dev->device.driver);
1973         if (!drv)
1974                         goto away;
1975
1976         if (dev->pausing || dev->resuming)
1977                         goto away;
1978
1979         /* Note that even though both drv->pause() and drv->resume
1980          * specify a callback function, it is NOT necessary for us to
1981          * increment our local module usage count.  Reason is, there
1982          * is already a linkage dependency between child function
1983          * drivers and visorbus, so it is already IMPOSSIBLE to unload
1984          * visorbus while child function drivers are still running.
1985          */
1986         if (is_pause) {
1987                 if (!drv->pause)
1988                                 goto away;
1989
1990                 dev->pausing = true;
1991                 x = drv->pause(dev, pause_state_change_complete);
1992         } else {
1993                 /* This should be done at BUS resume time, but an
1994                  * existing problem prevents us from ever getting a bus
1995                  * resume...  This hack would fail to work should we
1996                  * ever have a bus that contains NO devices, since we
1997                  * would never even get here in that case. */
1998                 fix_vbus_dev_info(dev);
1999                 if (!drv->resume)
2000                                 goto away;
2001
2002                 dev->resuming = true;
2003                 x = drv->resume(dev, resume_state_change_complete);
2004         }
2005         if (x < 0) {
2006                 if (is_pause)
2007                         dev->pausing = false;
2008                 else
2009                         dev->resuming = false;
2010                 goto away;
2011         }
2012         rc = 0;
2013 away:
2014         if (rc < 0) {
2015                 if (notify_func)
2016                                 (*notify_func)(bus_no, dev_no, rc);
2017         }
2018 }
2019
2020 static void
2021 chipset_device_pause(u32 bus_no, u32 dev_no)
2022 {
2023         initiate_chipset_device_pause_resume(bus_no, dev_no, true);
2024 }
2025
2026 static void
2027 chipset_device_resume(u32 bus_no, u32 dev_no)
2028 {
2029         initiate_chipset_device_pause_resume(bus_no, dev_no, false);
2030 }
2031
2032 struct channel_size_info {
2033         uuid_le guid;
2034         unsigned long min_size;
2035         unsigned long max_size;
2036 };
2037
2038 int
2039 visorbus_init(void)
2040 {
2041         int rc = 0;
2042
2043         POSTCODE_LINUX_3(DRIVER_ENTRY_PC, rc, POSTCODE_SEVERITY_INFO);
2044         bus_device_info_init(&clientbus_driverinfo,
2045                              "clientbus", "visorbus",
2046                              VERSION, NULL);
2047
2048         /* process module options */
2049
2050         if (visorbus_devicetest > MAXDEVICETEST)
2051                         visorbus_devicetest = MAXDEVICETEST;
2052
2053         rc = create_bus_type();
2054         if (rc < 0) {
2055                 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
2056                 goto away;
2057         }
2058
2059         periodic_dev_workqueue = create_singlethread_workqueue("visorbus_dev");
2060         if (!periodic_dev_workqueue) {
2061                 POSTCODE_LINUX_2(CREATE_WORKQUEUE_PC, DIAG_SEVERITY_ERR);
2062                 rc = -ENOMEM;
2063                 goto away;
2064         }
2065
2066         /* This enables us to receive notifications when devices appear for
2067          * which this service partition is to be a server for.
2068          */
2069         visorchipset_register_busdev(&chipset_notifiers,
2070                                      &chipset_responders,
2071                                      &chipset_driverinfo);
2072
2073         rc = 0;
2074
2075 away:
2076         if (rc)
2077                         POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2078                                          POSTCODE_SEVERITY_ERR);
2079         return rc;
2080 }
2081
2082 void
2083 visorbus_exit(void)
2084 {
2085         struct list_head *listentry, *listtmp;
2086
2087         visorchipset_register_busdev(NULL, NULL, NULL);
2088         remove_all_visor_devices();
2089
2090         flush_workqueue(periodic_dev_workqueue); /* better not be any work! */
2091         destroy_workqueue(periodic_dev_workqueue);
2092         periodic_dev_workqueue = NULL;
2093
2094         if (periodic_test_workqueue) {
2095                 cancel_delayed_work(&periodic_work);
2096                 flush_workqueue(periodic_test_workqueue);
2097                 destroy_workqueue(periodic_test_workqueue);
2098                 periodic_test_workqueue = NULL;
2099         }
2100
2101         list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
2102                 struct visorbus_devdata *devdata = list_entry(listentry,
2103                                                               struct
2104                                                               visorbus_devdata,
2105                                                               list_all);
2106                 remove_bus_instance(devdata);
2107         }
2108         remove_bus_type();
2109 }
2110
2111 module_param_named(debug, visorbus_debug, int, S_IRUGO);
2112 MODULE_PARM_DESC(visorbus_debug, "1 to debug");
2113 int visorbus_debug = 0;
2114
2115 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
2116 MODULE_PARM_DESC(visorbus_forcematch,
2117                  "1 to force a successful dev <--> drv match");
2118 int visorbus_forcematch = 0;
2119
2120 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
2121 MODULE_PARM_DESC(visorbus_forcenomatch,
2122                  "1 to force an UNsuccessful dev <--> drv match");
2123 int visorbus_forcenomatch = 0;
2124
2125 module_param_named(devicetest, visorbus_devicetest, int, S_IRUGO);
2126 MODULE_PARM_DESC(visorbus_devicetest,
2127                  "non-0 to just test device creation and destruction");
2128 int visorbus_devicetest = 0;
2129
2130 module_param_named(debugref, visorbus_debugref, int, S_IRUGO);
2131 MODULE_PARM_DESC(visorbus_debugref, "1 to debug reference counting");
2132 int visorbus_debugref = 0;
2133
2134 MODULE_AUTHOR("Unisys");
2135 MODULE_LICENSE("GPL");
2136 MODULE_DESCRIPTION("Supervisor bus driver for service partition: ver " VERSION);
2137 MODULE_VERSION(VERSION);