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