GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / staging / unisys / visorbus / visorbus_main.c
1 /*
2  * Copyright � 2010 - 2015 UNISYS CORPORATION
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  * NON INFRINGEMENT.  See the GNU General Public License for more
13  * details.
14  */
15
16 #include <linux/debugfs.h>
17 #include <linux/uuid.h>
18
19 #include "visorbus.h"
20 #include "visorbus_private.h"
21
22 static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
23
24 /* Display string that is guaranteed to be no longer the 99 characters */
25 #define LINESIZE 99
26 #define POLLJIFFIES_NORMALCHANNEL 10
27
28 /* stores whether bus_registration was successful */
29 static bool initialized;
30 static struct dentry *visorbus_debugfs_dir;
31
32 /*
33  * DEVICE type attributes
34  *
35  * The modalias file will contain the guid of the device.
36  */
37 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
38                              char *buf)
39 {
40         struct visor_device *vdev;
41         const guid_t *guid;
42
43         vdev = to_visor_device(dev);
44         guid = visorchannel_get_guid(vdev->visorchannel);
45         return sprintf(buf, "visorbus:%pUl\n", guid);
46 }
47 static DEVICE_ATTR_RO(modalias);
48
49 static struct attribute *visorbus_dev_attrs[] = {
50         &dev_attr_modalias.attr,
51         NULL,
52 };
53
54 ATTRIBUTE_GROUPS(visorbus_dev);
55
56 /* filled in with info about parent chipset driver when we register with it */
57 static struct visor_vbus_deviceinfo chipset_driverinfo;
58 /* filled in with info about this driver, wrt it servicing client busses */
59 static struct visor_vbus_deviceinfo clientbus_driverinfo;
60
61 /* list of visor_device structs, linked via .list_all */
62 static LIST_HEAD(list_all_bus_instances);
63 /* list of visor_device structs, linked via .list_all */
64 static LIST_HEAD(list_all_device_instances);
65
66 /*
67  * Generic function useful for validating any type of channel when it is
68  * received by the client that will be accessing the channel.
69  * Note that <logCtx> is only needed for callers in the EFI environment, and
70  * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
71  */
72 int visor_check_channel(struct channel_header *ch,
73                         struct device *dev,
74                         const guid_t *expected_guid,
75                         char *chname,
76                         u64 expected_min_bytes,
77                         u32 expected_version,
78                         u64 expected_signature)
79 {
80         if (!guid_is_null(expected_guid)) {
81                 /* caller wants us to verify type GUID */
82                 if (!guid_equal(&ch->chtype, expected_guid)) {
83                         dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
84                                 chname, expected_guid, expected_guid,
85                                 &ch->chtype);
86                         return 0;
87                 }
88         }
89         /* verify channel size */
90         if (expected_min_bytes > 0) {
91                 if (ch->size < expected_min_bytes) {
92                         dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
93                                 chname, expected_guid,
94                                 (unsigned long long)expected_min_bytes,
95                                 ch->size);
96                         return 0;
97                 }
98         }
99         /* verify channel version */
100         if (expected_version > 0) {
101                 if (ch->version_id != expected_version) {
102                         dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
103                                 chname, expected_guid,
104                                 (unsigned long)expected_version,
105                                 ch->version_id);
106                         return 0;
107                 }
108         }
109         /* verify channel signature */
110         if (expected_signature > 0) {
111                 if (ch->signature != expected_signature) {
112                         dev_err(dev, "Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
113                                 chname, expected_guid,  expected_signature,
114                                 ch->signature);
115                         return 0;
116                 }
117         }
118         return 1;
119 }
120
121 static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
122 {
123         struct visor_device *dev;
124         const guid_t *guid;
125
126         dev = to_visor_device(xdev);
127         guid = visorchannel_get_guid(dev->visorchannel);
128
129         return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
130 }
131
132 /*
133  * visorbus_match() - called automatically upon adding a visor_device
134  *                    (device_add), or adding a visor_driver
135  *                    (visorbus_register_visor_driver)
136  * @xdev: struct device for the device being matched
137  * @xdrv: struct device_driver for driver to match device against
138  *
139  * Return: 1 iff the provided driver can control the specified device
140  */
141 static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
142 {
143         const guid_t *channel_type;
144         int i;
145         struct visor_device *dev;
146         struct visor_driver *drv;
147
148         dev = to_visor_device(xdev);
149         channel_type = visorchannel_get_guid(dev->visorchannel);
150         drv = to_visor_driver(xdrv);
151         if (!drv->channel_types)
152                 return 0;
153
154         for (i = 0; !guid_is_null(&drv->channel_types[i].guid); i++)
155                 if (guid_equal(&drv->channel_types[i].guid, channel_type))
156                         return i + 1;
157
158         return 0;
159 }
160
161 /*
162  * This describes the TYPE of bus.
163  * (Don't confuse this with an INSTANCE of the bus.)
164  */
165 struct bus_type visorbus_type = {
166         .name = "visorbus",
167         .match = visorbus_match,
168         .uevent = visorbus_uevent,
169         .dev_groups = visorbus_dev_groups,
170 };
171
172 /*
173  * visorbus_release_busdevice() - called when device_unregister() is called for
174  *                                the bus device instance, after all other tasks
175  *                                involved with destroying the dev are complete
176  * @xdev: struct device for the bus being released
177  */
178 static void visorbus_release_busdevice(struct device *xdev)
179 {
180         struct visor_device *dev = dev_get_drvdata(xdev);
181
182         debugfs_remove(dev->debugfs_client_bus_info);
183         debugfs_remove_recursive(dev->debugfs_dir);
184         kfree(dev);
185 }
186
187 /*
188  * visorbus_release_device() - called when device_unregister() is called for
189  *                             each child device instance
190  * @xdev: struct device for the visor device being released
191  */
192 static void visorbus_release_device(struct device *xdev)
193 {
194         struct visor_device *dev = to_visor_device(xdev);
195
196         visorchannel_destroy(dev->visorchannel);
197         kfree(dev);
198 }
199
200 /*
201  * begin implementation of specific channel attributes to appear under
202  * /sys/bus/visorbus<x>/dev<y>/channel
203  */
204
205 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
206                              char *buf)
207 {
208         struct visor_device *vdev = to_visor_device(dev);
209
210         return sprintf(buf, "0x%llx\n",
211                        visorchannel_get_physaddr(vdev->visorchannel));
212 }
213 static DEVICE_ATTR_RO(physaddr);
214
215 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
216                            char *buf)
217 {
218         struct visor_device *vdev = to_visor_device(dev);
219
220         return sprintf(buf, "0x%lx\n",
221                         visorchannel_get_nbytes(vdev->visorchannel));
222 }
223 static DEVICE_ATTR_RO(nbytes);
224
225 static ssize_t clientpartition_show(struct device *dev,
226                                     struct device_attribute *attr, char *buf)
227 {
228         struct visor_device *vdev = to_visor_device(dev);
229
230         return sprintf(buf, "0x%llx\n",
231                        visorchannel_get_clientpartition(vdev->visorchannel));
232 }
233 static DEVICE_ATTR_RO(clientpartition);
234
235 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
236                              char *buf)
237 {
238         struct visor_device *vdev = to_visor_device(dev);
239         char typeid[LINESIZE];
240
241         return sprintf(buf, "%s\n",
242                        visorchannel_id(vdev->visorchannel, typeid));
243 }
244 static DEVICE_ATTR_RO(typeguid);
245
246 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
247                              char *buf)
248 {
249         struct visor_device *vdev = to_visor_device(dev);
250         char zoneid[LINESIZE];
251
252         return sprintf(buf, "%s\n",
253                        visorchannel_zoneid(vdev->visorchannel, zoneid));
254 }
255 static DEVICE_ATTR_RO(zoneguid);
256
257 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
258                              char *buf)
259 {
260         int i = 0;
261         struct bus_type *xbus = dev->bus;
262         struct device_driver *xdrv = dev->driver;
263         struct visor_driver *drv = NULL;
264
265         if (!xdrv)
266                 return 0;
267         i = xbus->match(dev, xdrv);
268         if (!i)
269                 return 0;
270         drv = to_visor_driver(xdrv);
271         return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
272 }
273 static DEVICE_ATTR_RO(typename);
274
275 static struct attribute *channel_attrs[] = {
276         &dev_attr_physaddr.attr,
277         &dev_attr_nbytes.attr,
278         &dev_attr_clientpartition.attr,
279         &dev_attr_typeguid.attr,
280         &dev_attr_zoneguid.attr,
281         &dev_attr_typename.attr,
282         NULL
283 };
284
285 ATTRIBUTE_GROUPS(channel);
286
287 /* end implementation of specific channel attributes */
288
289 /*
290  *  BUS instance attributes
291  *
292  *  define & implement display of bus attributes under
293  *  /sys/bus/visorbus/devices/visorbus<n>.
294  */
295
296 static ssize_t partition_handle_show(struct device *dev,
297                                      struct device_attribute *attr,
298                                      char *buf)
299 {
300         struct visor_device *vdev = to_visor_device(dev);
301         u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
302
303         return sprintf(buf, "0x%llx\n", handle);
304 }
305 static DEVICE_ATTR_RO(partition_handle);
306
307 static ssize_t partition_guid_show(struct device *dev,
308                                    struct device_attribute *attr,
309                                    char *buf)
310 {
311         struct visor_device *vdev = to_visor_device(dev);
312
313         return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
314 }
315 static DEVICE_ATTR_RO(partition_guid);
316
317 static ssize_t partition_name_show(struct device *dev,
318                                    struct device_attribute *attr,
319                                    char *buf)
320 {
321         struct visor_device *vdev = to_visor_device(dev);
322
323         return sprintf(buf, "%s\n", vdev->name);
324 }
325 static DEVICE_ATTR_RO(partition_name);
326
327 static ssize_t channel_addr_show(struct device *dev,
328                                  struct device_attribute *attr,
329                                  char *buf)
330 {
331         struct visor_device *vdev = to_visor_device(dev);
332         u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
333
334         return sprintf(buf, "0x%llx\n", addr);
335 }
336 static DEVICE_ATTR_RO(channel_addr);
337
338 static ssize_t channel_bytes_show(struct device *dev,
339                                   struct device_attribute *attr,
340                                   char *buf)
341 {
342         struct visor_device *vdev = to_visor_device(dev);
343         u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
344
345         return sprintf(buf, "0x%llx\n", nbytes);
346 }
347 static DEVICE_ATTR_RO(channel_bytes);
348
349 static ssize_t channel_id_show(struct device *dev,
350                                struct device_attribute *attr,
351                                char *buf)
352 {
353         struct visor_device *vdev = to_visor_device(dev);
354         int len = 0;
355
356         visorchannel_id(vdev->visorchannel, buf);
357         len = strlen(buf);
358         buf[len++] = '\n';
359
360         return len;
361 }
362 static DEVICE_ATTR_RO(channel_id);
363
364 static struct attribute *visorbus_attrs[] = {
365         &dev_attr_partition_handle.attr,
366         &dev_attr_partition_guid.attr,
367         &dev_attr_partition_name.attr,
368         &dev_attr_channel_addr.attr,
369         &dev_attr_channel_bytes.attr,
370         &dev_attr_channel_id.attr,
371         NULL
372 };
373
374 ATTRIBUTE_GROUPS(visorbus);
375
376 /*
377  *  BUS debugfs entries
378  *
379  *  define & implement display of debugfs attributes under
380  *  /sys/kernel/debug/visorbus/visorbus<n>.
381  */
382
383 /*
384  * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
385  *                               and write it to a seq_file
386  * @devinfo: the struct visor_vbus_deviceinfo to format
387  * @seq: seq_file to write to
388  * @devix: the device index to be included in the output data, or -1 if no
389  *         device index is to be included
390  *
391  * Reads @devInfo, and writes it in human-readable notation to @seq.
392  */
393 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
394                                       struct seq_file *seq, int devix)
395 {
396         /* uninitialized vbus device entry */
397         if (!isprint(devinfo->devtype[0]))
398                 return;
399
400         if (devix >= 0)
401                 seq_printf(seq, "[%d]", devix);
402         else
403                 /* vbus device entry is for bus or chipset */
404                 seq_puts(seq, "   ");
405
406         /*
407          * Note: because the s-Par back-end is free to scribble in this area,
408          * we never assume '\0'-termination.
409          */
410         seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
411                    (int)sizeof(devinfo->devtype), devinfo->devtype);
412         seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
413                    (int)sizeof(devinfo->drvname), devinfo->drvname);
414         seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
415                    devinfo->infostrs);
416 }
417
418 static int client_bus_info_debugfs_show(struct seq_file *seq, void *v)
419 {
420         int i = 0;
421         unsigned long off;
422         struct visor_vbus_deviceinfo dev_info;
423         struct visor_device *vdev = seq->private;
424         struct visorchannel *channel = vdev->visorchannel;
425
426         if (!channel)
427                 return 0;
428
429         seq_printf(seq,
430                    "Client device / client driver info for %s partition (vbus #%u):\n",
431                    ((vdev->name) ? (char *)(vdev->name) : ""),
432                    vdev->chipset_bus_no);
433
434         if (visorchannel_read(channel,
435                               offsetof(struct visor_vbus_channel, chp_info),
436                               &dev_info, sizeof(dev_info)) >= 0)
437                 vbuschannel_print_devinfo(&dev_info, seq, -1);
438         if (visorchannel_read(channel,
439                               offsetof(struct visor_vbus_channel, bus_info),
440                               &dev_info, sizeof(dev_info)) >= 0)
441                 vbuschannel_print_devinfo(&dev_info, seq, -1);
442
443         off = offsetof(struct visor_vbus_channel, dev_info);
444         while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
445                 if (visorchannel_read(channel, off, &dev_info,
446                                       sizeof(dev_info)) >= 0)
447                         vbuschannel_print_devinfo(&dev_info, seq, i);
448                 off += sizeof(dev_info);
449                 i++;
450         }
451
452         return 0;
453 }
454
455 static int client_bus_info_debugfs_open(struct inode *inode, struct file *file)
456 {
457         return single_open(file, client_bus_info_debugfs_show,
458                            inode->i_private);
459 }
460
461 static const struct file_operations client_bus_info_debugfs_fops = {
462         .owner = THIS_MODULE,
463         .open = client_bus_info_debugfs_open,
464         .read = seq_read,
465         .llseek = seq_lseek,
466         .release = single_release,
467 };
468
469 static void dev_periodic_work(unsigned long __opaque)
470 {
471         struct visor_device *dev = (struct visor_device *)__opaque;
472         struct visor_driver *drv = to_visor_driver(dev->device.driver);
473
474         drv->channel_interrupt(dev);
475         mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
476 }
477
478 static int dev_start_periodic_work(struct visor_device *dev)
479 {
480         if (dev->being_removed || dev->timer_active)
481                 return -EINVAL;
482         /* now up by at least 2 */
483         get_device(&dev->device);
484         dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
485         add_timer(&dev->timer);
486         dev->timer_active = true;
487         return 0;
488 }
489
490 static void dev_stop_periodic_work(struct visor_device *dev)
491 {
492         if (!dev->timer_active)
493                 return;
494         del_timer_sync(&dev->timer);
495         dev->timer_active = false;
496         put_device(&dev->device);
497 }
498
499 /*
500  * visordriver_remove_device() - handle visor device going away
501  * @xdev: struct device for the visor device being removed
502  *
503  * This is called when device_unregister() is called for each child device
504  * instance, to notify the appropriate visorbus function driver that the device
505  * is going away, and to decrease the reference count of the device.
506  *
507  * Return: 0 iff successful
508  */
509 static int visordriver_remove_device(struct device *xdev)
510 {
511         struct visor_device *dev;
512         struct visor_driver *drv;
513
514         dev = to_visor_device(xdev);
515         drv = to_visor_driver(xdev->driver);
516
517         mutex_lock(&dev->visordriver_callback_lock);
518         dev->being_removed = true;
519         drv->remove(dev);
520         mutex_unlock(&dev->visordriver_callback_lock);
521
522         dev_stop_periodic_work(dev);
523         put_device(&dev->device);
524
525         return 0;
526 }
527
528 /*
529  * visorbus_unregister_visor_driver() - unregisters the provided driver
530  * @drv: the driver to unregister
531  *
532  * A visor function driver calls this function to unregister the driver,
533  * i.e., within its module_exit function.
534  */
535 void visorbus_unregister_visor_driver(struct visor_driver *drv)
536 {
537         driver_unregister(&drv->driver);
538 }
539 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
540
541 /*
542  * visorbus_read_channel() - reads from the designated channel into
543  *                           the provided buffer
544  * @dev:    the device whose channel is read from
545  * @offset: the offset into the channel at which reading starts
546  * @dest:   the destination buffer that is written into from the channel
547  * @nbytes: the number of bytes to read from the channel
548  *
549  * If receiving a message, use the visorchannel_signalremove()
550  * function instead.
551  *
552  * Return: integer indicating success (zero) or failure (non-zero)
553  */
554 int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
555                           void *dest, unsigned long nbytes)
556 {
557         return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
558 }
559 EXPORT_SYMBOL_GPL(visorbus_read_channel);
560
561 /*
562  * visorbus_write_channel() - writes the provided buffer into the designated
563  *                            channel
564  * @dev:    the device whose channel is written to
565  * @offset: the offset into the channel at which writing starts
566  * @src:    the source buffer that is written into the channel
567  * @nbytes: the number of bytes to write into the channel
568  *
569  * If sending a message, use the visorchannel_signalinsert()
570  * function instead.
571  *
572  * Return: integer indicating success (zero) or failure (non-zero)
573  */
574 int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
575                            void *src, unsigned long nbytes)
576 {
577         return visorchannel_write(dev->visorchannel, offset, src, nbytes);
578 }
579 EXPORT_SYMBOL_GPL(visorbus_write_channel);
580
581 /*
582  * visorbus_enable_channel_interrupts() - enables interrupts on the
583  *                                        designated device
584  * @dev: the device on which to enable interrupts
585  *
586  * Currently we don't yet have a real interrupt, so for now we just call the
587  * interrupt function periodically via a timer.
588  */
589 int visorbus_enable_channel_interrupts(struct visor_device *dev)
590 {
591         struct visor_driver *drv = to_visor_driver(dev->device.driver);
592
593         if (!drv->channel_interrupt) {
594                 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
595                 return -ENOENT;
596         }
597
598         return dev_start_periodic_work(dev);
599 }
600 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
601
602 /*
603  * visorbus_disable_channel_interrupts() - disables interrupts on the
604  *                                         designated device
605  * @dev: the device on which to disable interrupts
606  */
607 void visorbus_disable_channel_interrupts(struct visor_device *dev)
608 {
609         dev_stop_periodic_work(dev);
610 }
611 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
612
613 /*
614  * create_visor_device() - create visor device as a result of receiving the
615  *                         controlvm device_create message for a new device
616  * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
617  *       for chipset_bus_no and chipset_dev_no, that will be initialized
618  *
619  * This is how everything starts from the device end.
620  * This function is called when a channel first appears via a ControlVM
621  * message.  In response, this function allocates a visor_device to
622  * correspond to the new channel, and attempts to connect it the appropriate
623  * driver.  If the appropriate driver is found, the visor_driver.probe()
624  * function for that driver will be called, and will be passed the new
625  * visor_device that we just created.
626  *
627  * It's ok if the appropriate driver is not yet loaded, because in that case
628  * the new device struct will just stick around in the bus' list of devices.
629  * When the appropriate driver calls visorbus_register_visor_driver(), the
630  * visor_driver.probe() for the new driver will be called with the new
631  * device.
632  *
633  * Return: 0 if successful, otherwise the negative value returned by
634  *         device_add() indicating the reason for failure
635  */
636 int create_visor_device(struct visor_device *dev)
637 {
638         int err;
639         u32 chipset_bus_no = dev->chipset_bus_no;
640         u32 chipset_dev_no = dev->chipset_dev_no;
641
642         mutex_init(&dev->visordriver_callback_lock);
643         dev->device.bus = &visorbus_type;
644         dev->device.groups = channel_groups;
645         device_initialize(&dev->device);
646         dev->device.release = visorbus_release_device;
647         /* keep a reference just for us (now 2) */
648         get_device(&dev->device);
649         setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev);
650
651         /*
652          * bus_id must be a unique name with respect to this bus TYPE
653          * (NOT bus instance).  That's why we need to include the bus
654          * number within the name.
655          */
656         err = dev_set_name(&dev->device, "vbus%u:dev%u",
657                            chipset_bus_no, chipset_dev_no);
658         if (err)
659                 goto err_put;
660
661         /*
662          * device_add does this:
663          *    bus_add_device(dev)
664          *    ->device_attach(dev)
665          *      ->for each driver drv registered on the bus that dev is on
666          *          if (dev.drv)  **  device already has a driver **
667          *            ** not sure we could ever get here... **
668          *          else
669          *            if (bus.match(dev,drv)) [visorbus_match]
670          *              dev.drv = drv
671          *              if (!drv.probe(dev))  [visordriver_probe_device]
672          *                dev.drv = NULL
673          *
674          *  Note that device_add does NOT fail if no driver failed to
675          *  claim the device.  The device will be linked onto
676          *  bus_type.klist_devices regardless (use bus_for_each_dev).
677          */
678         err = device_add(&dev->device);
679         if (err < 0)
680                 goto err_put;
681
682         list_add_tail(&dev->list_all, &list_all_device_instances);
683         dev->state.created = 1;
684         visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
685         /* success: reference kept via unmatched get_device() */
686         return 0;
687
688 err_put:
689         put_device(&dev->device);
690         dev_err(&dev->device, "Creating visor device failed. %d\n", err);
691         return err;
692 }
693
694 void remove_visor_device(struct visor_device *dev)
695 {
696         list_del(&dev->list_all);
697         put_device(&dev->device);
698         device_unregister(&dev->device);
699         visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
700 }
701
702 static int get_vbus_header_info(struct visorchannel *chan,
703                                 struct device *dev,
704                                 struct visor_vbus_headerinfo *hdr_info)
705 {
706         int err;
707
708         if (!visor_check_channel(visorchannel_get_header(chan),
709                                  dev,
710                                  &visor_vbus_channel_guid,
711                                  "vbus",
712                                  sizeof(struct visor_vbus_channel),
713                                  VISOR_VBUS_CHANNEL_VERSIONID,
714                                  VISOR_CHANNEL_SIGNATURE))
715                 return -EINVAL;
716
717         err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
718                                 sizeof(*hdr_info));
719         if (err < 0)
720                 return err;
721
722         if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
723                 return -EINVAL;
724
725         if (hdr_info->device_info_struct_bytes <
726             sizeof(struct visor_vbus_deviceinfo))
727                 return -EINVAL;
728
729         return 0;
730 }
731
732 /*
733  * write_vbus_chp_info() - write the contents of <info> to the struct
734  *                         visor_vbus_channel.chp_info
735  * @chan:     indentifies the s-Par channel that will be updated
736  * @hdr_info: used to find appropriate channel offset to write data
737  * @info:     contains the information to write
738  *
739  * Writes chipset info into the channel memory to be used for diagnostic
740  * purposes.
741  *
742  * Returns no value since this is debug information and not needed for
743  * device functionality.
744  */
745 static void write_vbus_chp_info(struct visorchannel *chan,
746                                 struct visor_vbus_headerinfo *hdr_info,
747                                 struct visor_vbus_deviceinfo *info)
748 {
749         int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
750
751         if (hdr_info->chp_info_offset == 0)
752                 return;
753
754         visorchannel_write(chan, off, info, sizeof(*info));
755 }
756
757 /*
758  * write_vbus_bus_info() - write the contents of <info> to the struct
759  *                         visor_vbus_channel.bus_info
760  * @chan:     indentifies the s-Par channel that will be updated
761  * @hdr_info: used to find appropriate channel offset to write data
762  * @info:     contains the information to write
763  *
764  * Writes bus info into the channel memory to be used for diagnostic
765  * purposes.
766  *
767  * Returns no value since this is debug information and not needed for
768  * device functionality.
769  */
770 static void write_vbus_bus_info(struct visorchannel *chan,
771                                 struct visor_vbus_headerinfo *hdr_info,
772                                 struct visor_vbus_deviceinfo *info)
773 {
774         int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
775
776         if (hdr_info->bus_info_offset == 0)
777                 return;
778
779         visorchannel_write(chan, off, info, sizeof(*info));
780 }
781
782 /*
783  * write_vbus_dev_info() - write the contents of <info> to the struct
784  *                         visor_vbus_channel.dev_info[<devix>]
785  * @chan:     indentifies the s-Par channel that will be updated
786  * @hdr_info: used to find appropriate channel offset to write data
787  * @info:     contains the information to write
788  * @devix:    the relative device number (0..n-1) of the device on the bus
789  *
790  * Writes device info into the channel memory to be used for diagnostic
791  * purposes.
792  *
793  * Returns no value since this is debug information and not needed for
794  * device functionality.
795  */
796 static void write_vbus_dev_info(struct visorchannel *chan,
797                                 struct visor_vbus_headerinfo *hdr_info,
798                                 struct visor_vbus_deviceinfo *info,
799                                 unsigned int devix)
800 {
801         int off =
802             (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
803             (hdr_info->device_info_struct_bytes * devix);
804
805         if (hdr_info->dev_info_offset == 0)
806                 return;
807
808         visorchannel_write(chan, off, info, sizeof(*info));
809 }
810
811 static void bus_device_info_init(
812                 struct visor_vbus_deviceinfo *bus_device_info_ptr,
813                 const char *dev_type, const char *drv_name)
814 {
815         memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
816         snprintf(bus_device_info_ptr->devtype,
817                  sizeof(bus_device_info_ptr->devtype),
818                  "%s", (dev_type) ? dev_type : "unknownType");
819         snprintf(bus_device_info_ptr->drvname,
820                  sizeof(bus_device_info_ptr->drvname),
821                  "%s", (drv_name) ? drv_name : "unknownDriver");
822         snprintf(bus_device_info_ptr->infostrs,
823                  sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s",
824                  utsname()->release);
825 }
826
827 /*
828  * publish_vbus_dev_info() - for a child device just created on a client bus,
829  *                           fill in information about the driver that is
830  *                           controlling this device into the appropriate slot
831  *                           within the vbus channel of the bus instance
832  * @visordev: struct visor_device for the desired device
833  */
834 static void publish_vbus_dev_info(struct visor_device *visordev)
835 {
836         int i;
837         struct visor_device *bdev;
838         struct visor_driver *visordrv;
839         u32 bus_no = visordev->chipset_bus_no;
840         u32 dev_no = visordev->chipset_dev_no;
841         struct visor_vbus_deviceinfo dev_info;
842         const char *chan_type_name = NULL;
843         struct visor_vbus_headerinfo *hdr_info;
844
845         if (!visordev->device.driver)
846                 return;
847
848         bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
849         if (!bdev)
850                 return;
851         hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
852         if (!hdr_info)
853                 return;
854         visordrv = to_visor_driver(visordev->device.driver);
855
856         /*
857          * Within the list of device types (by GUID) that the driver
858          * says it supports, find out which one of those types matches
859          * the type of this device, so that we can include the device
860          * type name
861          */
862         for (i = 0; visordrv->channel_types[i].name; i++) {
863                 if (memcmp(&visordrv->channel_types[i].guid,
864                            &visordev->channel_type_guid,
865                            sizeof(visordrv->channel_types[i].guid)) == 0) {
866                         chan_type_name = visordrv->channel_types[i].name;
867                         break;
868                 }
869         }
870
871         bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
872         write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
873         write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
874         write_vbus_bus_info(bdev->visorchannel, hdr_info,
875                             &clientbus_driverinfo);
876 }
877
878 /*
879  * visordriver_probe_device() - handle new visor device coming online
880  * @xdev: struct device for the visor device being probed
881  *
882  * This is called automatically upon adding a visor_device (device_add), or
883  * adding a visor_driver (visorbus_register_visor_driver), but only after
884  * visorbus_match() has returned 1 to indicate a successful match between
885  * driver and device.
886  *
887  * If successful, a reference to the device will be held onto via get_device().
888  *
889  * Return: 0 if successful, meaning the function driver's probe() function
890  *         was successful with this device, otherwise a negative errno
891  *         value indicating failure reason
892  */
893 static int visordriver_probe_device(struct device *xdev)
894 {
895         int res;
896         struct visor_driver *drv;
897         struct visor_device *dev;
898
899         dev = to_visor_device(xdev);
900         drv = to_visor_driver(xdev->driver);
901
902         mutex_lock(&dev->visordriver_callback_lock);
903         dev->being_removed = false;
904
905         res = drv->probe(dev);
906         if (res >= 0) {
907                 /* success: reference kept via unmatched get_device() */
908                 get_device(&dev->device);
909                 publish_vbus_dev_info(dev);
910         }
911
912         mutex_unlock(&dev->visordriver_callback_lock);
913         return res;
914 }
915
916 /*
917  * visorbus_register_visor_driver() - registers the provided visor driver
918  *                                    for handling one or more visor device
919  *                                    types (channel_types)
920  * @drv: the driver to register
921  *
922  * A visor function driver calls this function to register
923  * the driver.  The caller MUST fill in the following fields within the
924  * #drv structure:
925  *     name, version, owner, channel_types, probe, remove
926  *
927  * Here's how the whole Linux bus / driver / device model works.
928  *
929  * At system start-up, the visorbus kernel module is loaded, which registers
930  * visorbus_type as a bus type, using bus_register().
931  *
932  * All kernel modules that support particular device types on a
933  * visorbus bus are loaded.  Each of these kernel modules calls
934  * visorbus_register_visor_driver() in their init functions, passing a
935  * visor_driver struct.  visorbus_register_visor_driver() in turn calls
936  * register_driver(&visor_driver.driver).  This .driver member is
937  * initialized with generic methods (like probe), whose sole responsibility
938  * is to act as a broker for the real methods, which are within the
939  * visor_driver struct.  (This is the way the subclass behavior is
940  * implemented, since visor_driver is essentially a subclass of the
941  * generic driver.)  Whenever a driver_register() happens, core bus code in
942  * the kernel does (see device_attach() in drivers/base/dd.c):
943  *
944  *     for each dev associated with the bus (the bus that driver is on) that
945  *     does not yet have a driver
946  *         if bus.match(dev,newdriver) == yes_matched  ** .match specified
947  *                                                ** during bus_register().
948  *             newdriver.probe(dev)  ** for visor drivers, this will call
949  *                   ** the generic driver.probe implemented in visorbus.c,
950  *                   ** which in turn calls the probe specified within the
951  *                   ** struct visor_driver (which was specified by the
952  *                   ** actual device driver as part of
953  *                   ** visorbus_register_visor_driver()).
954  *
955  * The above dance also happens when a new device appears.
956  * So the question is, how are devices created within the system?
957  * Basically, just call device_add(dev).  See pci_bus_add_devices().
958  * pci_scan_device() shows an example of how to build a device struct.  It
959  * returns the newly-created struct to pci_scan_single_device(), who adds it
960  * to the list of devices at PCIBUS.devices.  That list of devices is what
961  * is traversed by pci_bus_add_devices().
962  *
963  * Return: integer indicating success (zero) or failure (non-zero)
964  */
965 int visorbus_register_visor_driver(struct visor_driver *drv)
966 {
967         /* can't register on a nonexistent bus */
968         if (!initialized)
969                 return -ENODEV;
970
971         if (!drv->probe)
972                 return -EINVAL;
973
974         if (!drv->remove)
975                 return -EINVAL;
976
977         if (!drv->pause)
978                 return -EINVAL;
979
980         if (!drv->resume)
981                 return -EINVAL;
982
983         drv->driver.name = drv->name;
984         drv->driver.bus = &visorbus_type;
985         drv->driver.probe = visordriver_probe_device;
986         drv->driver.remove = visordriver_remove_device;
987         drv->driver.owner = drv->owner;
988
989         /*
990          * driver_register does this:
991          *   bus_add_driver(drv)
992          *   ->if (drv.bus)  ** (bus_type) **
993          *       driver_attach(drv)
994          *         for each dev with bus type of drv.bus
995          *           if (!dev.drv)  ** no driver assigned yet **
996          *             if (bus.match(dev,drv))  [visorbus_match]
997          *               dev.drv = drv
998          *               if (!drv.probe(dev))   [visordriver_probe_device]
999          *                 dev.drv = NULL
1000          */
1001
1002         return driver_register(&drv->driver);
1003 }
1004 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1005
1006 /*
1007  * visorbus_create_instance() - create a device instance for the visorbus itself
1008  * @dev: struct visor_device indicating the bus instance
1009  *
1010  * Return: 0 for success, otherwise negative errno value indicating reason for
1011  *         failure
1012  */
1013 int visorbus_create_instance(struct visor_device *dev)
1014 {
1015         int id = dev->chipset_bus_no;
1016         int err;
1017         struct visor_vbus_headerinfo *hdr_info;
1018
1019         hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
1020         if (!hdr_info)
1021                 return -ENOMEM;
1022
1023         dev_set_name(&dev->device, "visorbus%d", id);
1024         dev->device.bus = &visorbus_type;
1025         dev->device.groups = visorbus_groups;
1026         dev->device.release = visorbus_release_busdevice;
1027
1028         dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1029                                               visorbus_debugfs_dir);
1030         dev->debugfs_client_bus_info =
1031                 debugfs_create_file("client_bus_info", 0440,
1032                                     dev->debugfs_dir, dev,
1033                                     &client_bus_info_debugfs_fops);
1034
1035         dev_set_drvdata(&dev->device, dev);
1036         err = get_vbus_header_info(dev->visorchannel, &dev->device, hdr_info);
1037         if (err < 0)
1038                 goto err_debugfs_dir;
1039
1040         err = device_register(&dev->device);
1041         if (err < 0)
1042                 goto err_debugfs_dir;
1043
1044         list_add_tail(&dev->list_all, &list_all_bus_instances);
1045
1046         dev->state.created = 1;
1047         dev->vbus_hdr_info = (void *)hdr_info;
1048         write_vbus_chp_info(dev->visorchannel, hdr_info,
1049                             &chipset_driverinfo);
1050         write_vbus_bus_info(dev->visorchannel, hdr_info,
1051                             &clientbus_driverinfo);
1052
1053         visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
1054
1055         return 0;
1056
1057 err_debugfs_dir:
1058         debugfs_remove_recursive(dev->debugfs_dir);
1059         kfree(hdr_info);
1060         dev_err(&dev->device, "%s failed: %d\n", __func__, err);
1061         return err;
1062 }
1063
1064 /*
1065  * visorbus_remove_instance() - remove a device instance for the visorbus itself
1066  * @dev: struct visor_device indentifying the bus to remove
1067  */
1068 void visorbus_remove_instance(struct visor_device *dev)
1069 {
1070         /*
1071          * Note that this will result in the release method for
1072          * dev->dev being called, which will call
1073          * visorbus_release_busdevice().  This has something to do with
1074          * the put_device() done in device_unregister(), but I have never
1075          * successfully been able to trace thru the code to see where/how
1076          * release() gets called.  But I know it does.
1077          */
1078         visorchannel_destroy(dev->visorchannel);
1079         kfree(dev->vbus_hdr_info);
1080         list_del(&dev->list_all);
1081         device_unregister(&dev->device);
1082         visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
1083 }
1084
1085 /*
1086  * remove_all_visor_devices() - remove all child visorbus device instances
1087  */
1088 static void remove_all_visor_devices(void)
1089 {
1090         struct list_head *listentry, *listtmp;
1091
1092         list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1093                 struct visor_device *dev = list_entry(listentry,
1094                                                       struct visor_device,
1095                                                       list_all);
1096                 remove_visor_device(dev);
1097         }
1098 }
1099
1100 /*
1101  * pause_state_change_complete() - the callback function to be called by a
1102  *                                 visorbus function driver when a
1103  *                                 pending "pause device" operation has
1104  *                                 completed
1105  * @dev: struct visor_device identifying the paused device
1106  * @status: 0 iff the pause state change completed successfully, otherwise
1107  *          a negative errno value indicating the reason for failure
1108  */
1109 static void pause_state_change_complete(struct visor_device *dev, int status)
1110 {
1111         if (!dev->pausing)
1112                 return;
1113
1114         dev->pausing = false;
1115         visorbus_device_changestate_response(dev, status,
1116                                              segment_state_standby);
1117 }
1118
1119 /*
1120  * resume_state_change_complete() - the callback function to be called by a
1121  *                                  visorbus function driver when a
1122  *                                  pending "resume device" operation has
1123  *                                  completed
1124  * @dev: struct visor_device identifying the resumed device
1125  * @status: 0 iff the resume state change completed successfully, otherwise
1126  *          a negative errno value indicating the reason for failure
1127  */
1128 static void resume_state_change_complete(struct visor_device *dev, int status)
1129 {
1130         if (!dev->resuming)
1131                 return;
1132
1133         dev->resuming = false;
1134
1135         /*
1136          * Notify the chipset driver that the resume is complete,
1137          * which will presumably want to send some sort of response to
1138          * the initiator.
1139          */
1140         visorbus_device_changestate_response(dev, status,
1141                                              segment_state_running);
1142 }
1143
1144 /*
1145  * visorchipset_initiate_device_pause_resume() - start a pause or resume
1146  *                                               operation for a visor device
1147  * @dev: struct visor_device identifying the device being paused or resumed
1148  * @is_pause: true to indicate pause operation, false to indicate resume
1149  *
1150  * Tell the subordinate function driver for a specific device to pause
1151  * or resume that device.  Success/failure result is returned asynchronously
1152  * via a callback function; see pause_state_change_complete() and
1153  * resume_state_change_complete().
1154  */
1155 static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1156                                                      bool is_pause)
1157 {
1158         int err;
1159         struct visor_driver *drv = NULL;
1160
1161         /* If no driver associated with the device nothing to pause/resume */
1162         if (!dev->device.driver)
1163                 return 0;
1164         if (dev->pausing || dev->resuming)
1165                 return -EBUSY;
1166
1167         drv = to_visor_driver(dev->device.driver);
1168         if (is_pause) {
1169                 dev->pausing = true;
1170                 err = drv->pause(dev, pause_state_change_complete);
1171         } else {
1172                 /*
1173                  * The vbus_dev_info structure in the channel was been cleared,
1174                  * make sure it is valid.
1175                  */
1176                 publish_vbus_dev_info(dev);
1177                 dev->resuming = true;
1178                 err = drv->resume(dev, resume_state_change_complete);
1179         }
1180
1181         return err;
1182 }
1183
1184 /*
1185  * visorchipset_device_pause() - start a pause operation for a visor device
1186  * @dev_info: struct visor_device identifying the device being paused
1187  *
1188  * Tell the subordinate function driver for a specific device to pause
1189  * that device.  Success/failure result is returned asynchronously
1190  * via a callback function; see pause_state_change_complete().
1191  */
1192 int visorchipset_device_pause(struct visor_device *dev_info)
1193 {
1194         int err;
1195
1196         err = visorchipset_initiate_device_pause_resume(dev_info, true);
1197         if (err < 0) {
1198                 dev_info->pausing = false;
1199                 return err;
1200         }
1201
1202         return 0;
1203 }
1204
1205 /*
1206  * visorchipset_device_resume() - start a resume operation for a visor device
1207  * @dev_info: struct visor_device identifying the device being resumed
1208  *
1209  * Tell the subordinate function driver for a specific device to resume
1210  * that device.  Success/failure result is returned asynchronously
1211  * via a callback function; see resume_state_change_complete().
1212  */
1213 int visorchipset_device_resume(struct visor_device *dev_info)
1214 {
1215         int err;
1216
1217         err = visorchipset_initiate_device_pause_resume(dev_info, false);
1218         if (err < 0) {
1219                 dev_info->resuming = false;
1220                 return err;
1221         }
1222
1223         return 0;
1224 }
1225
1226 int visorbus_init(void)
1227 {
1228         int err;
1229
1230         visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1231         if (!visorbus_debugfs_dir)
1232                 return -ENOMEM;
1233
1234         bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1235
1236         err = bus_register(&visorbus_type);
1237         if (err < 0)
1238                 return err;
1239
1240         initialized = true;
1241         bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1242
1243         return 0;
1244 }
1245
1246 void visorbus_exit(void)
1247 {
1248         struct list_head *listentry, *listtmp;
1249
1250         remove_all_visor_devices();
1251
1252         list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1253                 struct visor_device *dev = list_entry(listentry,
1254                                                       struct visor_device,
1255                                                       list_all);
1256                 visorbus_remove_instance(dev);
1257         }
1258
1259         bus_unregister(&visorbus_type);
1260         initialized = false;
1261         debugfs_remove_recursive(visorbus_debugfs_dir);
1262 }