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