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