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