]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/unisys/visorbus/visorbus_main.c
staging: unisys: use ATTRIBUTE_GROUPS instead of creating our own
[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/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 const guid_t *expected_guid,
74 char *chname,
75 u64 expected_min_bytes,
76 u32 expected_version,
77 u64 expected_signature)
78 {
79 if (!guid_is_null(expected_guid)) {
80 /* caller wants us to verify type GUID */
81 if (!guid_equal(&ch->chtype, expected_guid)) {
82 pr_err("Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
83 chname, expected_guid,
84 expected_guid, &ch->chtype);
85 return 0;
86 }
87 }
88 /* verify channel size */
89 if (expected_min_bytes > 0) {
90 if (ch->size < expected_min_bytes) {
91 pr_err("Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
92 chname, expected_guid,
93 (unsigned long long)expected_min_bytes,
94 ch->size);
95 return 0;
96 }
97 }
98 /* verify channel version */
99 if (expected_version > 0) {
100 if (ch->version_id != expected_version) {
101 pr_err("Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8x\n",
102 chname, expected_guid,
103 (unsigned long)expected_version,
104 ch->version_id);
105 return 0;
106 }
107 }
108 /* verify channel signature */
109 if (expected_signature > 0) {
110 if (ch->signature != expected_signature) {
111 pr_err("Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
112 chname, expected_guid,
113 expected_signature, ch->signature);
114 return 0;
115 }
116 }
117 return 1;
118 }
119 EXPORT_SYMBOL_GPL(visor_check_channel);
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;
155 !guid_is_null(&drv->channel_types[i].guid) || drv->channel_types[i].name;
156 i++)
157 if (guid_equal(&drv->channel_types[i].guid, channel_type))
158 return i + 1;
159
160 return 0;
161 }
162
163 /*
164 * This describes the TYPE of bus.
165 * (Don't confuse this with an INSTANCE of the bus.)
166 */
167 struct bus_type visorbus_type = {
168 .name = "visorbus",
169 .match = visorbus_match,
170 .uevent = visorbus_uevent,
171 .dev_groups = visorbus_dev_groups,
172 };
173
174 /*
175 * visorbus_release_busdevice() - called when device_unregister() is called for
176 * the bus device instance, after all other tasks
177 * involved with destroying the dev are complete
178 * @xdev: struct device for the bus being released
179 */
180 static void visorbus_release_busdevice(struct device *xdev)
181 {
182 struct visor_device *dev = dev_get_drvdata(xdev);
183
184 debugfs_remove(dev->debugfs_client_bus_info);
185 debugfs_remove_recursive(dev->debugfs_dir);
186 kfree(dev);
187 }
188
189 /*
190 * visorbus_release_device() - called when device_unregister() is called for
191 * each child device instance
192 * @xdev: struct device for the visor device being released
193 */
194 static void visorbus_release_device(struct device *xdev)
195 {
196 struct visor_device *dev = to_visor_device(xdev);
197
198 visorchannel_destroy(dev->visorchannel);
199 kfree(dev);
200 }
201
202 /*
203 * begin implementation of specific channel attributes to appear under
204 * /sys/bus/visorbus<x>/dev<y>/channel
205 */
206
207 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
208 char *buf)
209 {
210 struct visor_device *vdev = to_visor_device(dev);
211
212 return sprintf(buf, "0x%llx\n",
213 visorchannel_get_physaddr(vdev->visorchannel));
214 }
215 static DEVICE_ATTR_RO(physaddr);
216
217 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
218 char *buf)
219 {
220 struct visor_device *vdev = to_visor_device(dev);
221
222 return sprintf(buf, "0x%lx\n",
223 visorchannel_get_nbytes(vdev->visorchannel));
224 }
225 static DEVICE_ATTR_RO(nbytes);
226
227 static ssize_t clientpartition_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229 {
230 struct visor_device *vdev = to_visor_device(dev);
231
232 return sprintf(buf, "0x%llx\n",
233 visorchannel_get_clientpartition(vdev->visorchannel));
234 }
235 static DEVICE_ATTR_RO(clientpartition);
236
237 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
238 char *buf)
239 {
240 struct visor_device *vdev = to_visor_device(dev);
241 char typeid[LINESIZE];
242
243 return sprintf(buf, "%s\n",
244 visorchannel_id(vdev->visorchannel, typeid));
245 }
246 static DEVICE_ATTR_RO(typeguid);
247
248 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
249 char *buf)
250 {
251 struct visor_device *vdev = to_visor_device(dev);
252 char zoneid[LINESIZE];
253
254 return sprintf(buf, "%s\n",
255 visorchannel_zoneid(vdev->visorchannel, zoneid));
256 }
257 static DEVICE_ATTR_RO(zoneguid);
258
259 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
260 char *buf)
261 {
262 int i = 0;
263 struct bus_type *xbus = dev->bus;
264 struct device_driver *xdrv = dev->driver;
265 struct visor_driver *drv = NULL;
266
267 if (!xdrv)
268 return 0;
269 i = xbus->match(dev, xdrv);
270 if (!i)
271 return 0;
272 drv = to_visor_driver(xdrv);
273 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
274 }
275 static DEVICE_ATTR_RO(typename);
276
277 static struct attribute *channel_attrs[] = {
278 &dev_attr_physaddr.attr,
279 &dev_attr_nbytes.attr,
280 &dev_attr_clientpartition.attr,
281 &dev_attr_typeguid.attr,
282 &dev_attr_zoneguid.attr,
283 &dev_attr_typename.attr,
284 NULL
285 };
286
287 ATTRIBUTE_GROUPS(channel);
288
289 /* end implementation of specific channel attributes */
290
291 /*
292 * BUS instance attributes
293 *
294 * define & implement display of bus attributes under
295 * /sys/bus/visorbus/devices/visorbus<n>.
296 */
297
298 static ssize_t partition_handle_show(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
301 {
302 struct visor_device *vdev = to_visor_device(dev);
303 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
304
305 return sprintf(buf, "0x%llx\n", handle);
306 }
307 static DEVICE_ATTR_RO(partition_handle);
308
309 static ssize_t partition_guid_show(struct device *dev,
310 struct device_attribute *attr,
311 char *buf)
312 {
313 struct visor_device *vdev = to_visor_device(dev);
314
315 return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
316 }
317 static DEVICE_ATTR_RO(partition_guid);
318
319 static ssize_t partition_name_show(struct device *dev,
320 struct device_attribute *attr,
321 char *buf)
322 {
323 struct visor_device *vdev = to_visor_device(dev);
324
325 return sprintf(buf, "%s\n", vdev->name);
326 }
327 static DEVICE_ATTR_RO(partition_name);
328
329 static ssize_t channel_addr_show(struct device *dev,
330 struct device_attribute *attr,
331 char *buf)
332 {
333 struct visor_device *vdev = to_visor_device(dev);
334 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
335
336 return sprintf(buf, "0x%llx\n", addr);
337 }
338 static DEVICE_ATTR_RO(channel_addr);
339
340 static ssize_t channel_bytes_show(struct device *dev,
341 struct device_attribute *attr,
342 char *buf)
343 {
344 struct visor_device *vdev = to_visor_device(dev);
345 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
346
347 return sprintf(buf, "0x%llx\n", nbytes);
348 }
349 static DEVICE_ATTR_RO(channel_bytes);
350
351 static ssize_t channel_id_show(struct device *dev,
352 struct device_attribute *attr,
353 char *buf)
354 {
355 struct visor_device *vdev = to_visor_device(dev);
356 int len = 0;
357
358 visorchannel_id(vdev->visorchannel, buf);
359 len = strlen(buf);
360 buf[len++] = '\n';
361
362 return len;
363 }
364 static DEVICE_ATTR_RO(channel_id);
365
366 static struct attribute *visorbus_attrs[] = {
367 &dev_attr_partition_handle.attr,
368 &dev_attr_partition_guid.attr,
369 &dev_attr_partition_name.attr,
370 &dev_attr_channel_addr.attr,
371 &dev_attr_channel_bytes.attr,
372 &dev_attr_channel_id.attr,
373 NULL
374 };
375
376 ATTRIBUTE_GROUPS(visorbus);
377
378 /*
379 * BUS debugfs entries
380 *
381 * define & implement display of debugfs attributes under
382 * /sys/kernel/debug/visorbus/visorbus<n>.
383 */
384
385 /*
386 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
387 * and write it to a seq_file
388 * @devinfo: the struct visor_vbus_deviceinfo to format
389 * @seq: seq_file to write to
390 * @devix: the device index to be included in the output data, or -1 if no
391 * device index is to be included
392 *
393 * Reads @devInfo, and writes it in human-readable notation to @seq.
394 */
395 static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
396 struct seq_file *seq, int devix)
397 {
398 /* uninitialized vbus device entry */
399 if (!isprint(devinfo->devtype[0]))
400 return;
401
402 if (devix >= 0)
403 seq_printf(seq, "[%d]", devix);
404 else
405 /* vbus device entry is for bus or chipset */
406 seq_puts(seq, " ");
407
408 /*
409 * Note: because the s-Par back-end is free to scribble in this area,
410 * we never assume '\0'-termination.
411 */
412 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype),
413 (int)sizeof(devinfo->devtype), devinfo->devtype);
414 seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname),
415 (int)sizeof(devinfo->drvname), devinfo->drvname);
416 seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs),
417 devinfo->infostrs);
418 }
419
420 static int client_bus_info_debugfs_show(struct seq_file *seq, void *v)
421 {
422 int i = 0;
423 unsigned long off;
424 struct visor_vbus_deviceinfo dev_info;
425 struct visor_device *vdev = seq->private;
426 struct visorchannel *channel = vdev->visorchannel;
427
428 if (!channel)
429 return 0;
430
431 seq_printf(seq,
432 "Client device / client driver info for %s partition (vbus #%u):\n",
433 ((vdev->name) ? (char *)(vdev->name) : ""),
434 vdev->chipset_bus_no);
435
436 if (visorchannel_read(channel,
437 offsetof(struct visor_vbus_channel, chp_info),
438 &dev_info, sizeof(dev_info)) >= 0)
439 vbuschannel_print_devinfo(&dev_info, seq, -1);
440 if (visorchannel_read(channel,
441 offsetof(struct visor_vbus_channel, bus_info),
442 &dev_info, sizeof(dev_info)) >= 0)
443 vbuschannel_print_devinfo(&dev_info, seq, -1);
444
445 off = offsetof(struct visor_vbus_channel, dev_info);
446 while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) {
447 if (visorchannel_read(channel, off, &dev_info,
448 sizeof(dev_info)) >= 0)
449 vbuschannel_print_devinfo(&dev_info, seq, i);
450 off += sizeof(dev_info);
451 i++;
452 }
453
454 return 0;
455 }
456
457 static int client_bus_info_debugfs_open(struct inode *inode, struct file *file)
458 {
459 return single_open(file, client_bus_info_debugfs_show,
460 inode->i_private);
461 }
462
463 static const struct file_operations client_bus_info_debugfs_fops = {
464 .owner = THIS_MODULE,
465 .open = client_bus_info_debugfs_open,
466 .read = seq_read,
467 .llseek = seq_lseek,
468 .release = single_release,
469 };
470
471 static void dev_periodic_work(unsigned long __opaque)
472 {
473 struct visor_device *dev = (struct visor_device *)__opaque;
474 struct visor_driver *drv = to_visor_driver(dev->device.driver);
475
476 drv->channel_interrupt(dev);
477 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
478 }
479
480 static int dev_start_periodic_work(struct visor_device *dev)
481 {
482 if (dev->being_removed || dev->timer_active)
483 return -EINVAL;
484 /* now up by at least 2 */
485 get_device(&dev->device);
486 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
487 add_timer(&dev->timer);
488 dev->timer_active = true;
489 return 0;
490 }
491
492 static void dev_stop_periodic_work(struct visor_device *dev)
493 {
494 if (!dev->timer_active)
495 return;
496 del_timer_sync(&dev->timer);
497 dev->timer_active = false;
498 put_device(&dev->device);
499 }
500
501 /*
502 * visordriver_remove_device() - handle visor device going away
503 * @xdev: struct device for the visor device being removed
504 *
505 * This is called when device_unregister() is called for each child device
506 * instance, to notify the appropriate visorbus function driver that the device
507 * is going away, and to decrease the reference count of the device.
508 *
509 * Return: 0 iff successful
510 */
511 static int visordriver_remove_device(struct device *xdev)
512 {
513 struct visor_device *dev;
514 struct visor_driver *drv;
515
516 dev = to_visor_device(xdev);
517 drv = to_visor_driver(xdev->driver);
518
519 mutex_lock(&dev->visordriver_callback_lock);
520 dev->being_removed = true;
521 drv->remove(dev);
522 mutex_unlock(&dev->visordriver_callback_lock);
523
524 dev_stop_periodic_work(dev);
525 put_device(&dev->device);
526
527 return 0;
528 }
529
530 /*
531 * visorbus_unregister_visor_driver() - unregisters the provided driver
532 * @drv: the driver to unregister
533 *
534 * A visor function driver calls this function to unregister the driver,
535 * i.e., within its module_exit function.
536 */
537 void visorbus_unregister_visor_driver(struct visor_driver *drv)
538 {
539 driver_unregister(&drv->driver);
540 }
541 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
542
543 /*
544 * visorbus_read_channel() - reads from the designated channel into
545 * the provided buffer
546 * @dev: the device whose channel is read from
547 * @offset: the offset into the channel at which reading starts
548 * @dest: the destination buffer that is written into from the channel
549 * @nbytes: the number of bytes to read from the channel
550 *
551 * If receiving a message, use the visorchannel_signalremove()
552 * function instead.
553 *
554 * Return: integer indicating success (zero) or failure (non-zero)
555 */
556 int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
557 void *dest, unsigned long nbytes)
558 {
559 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
560 }
561 EXPORT_SYMBOL_GPL(visorbus_read_channel);
562
563 /*
564 * visorbus_write_channel() - writes the provided buffer into the designated
565 * channel
566 * @dev: the device whose channel is written to
567 * @offset: the offset into the channel at which writing starts
568 * @src: the source buffer that is written into the channel
569 * @nbytes: the number of bytes to write into the channel
570 *
571 * If sending a message, use the visorchannel_signalinsert()
572 * function instead.
573 *
574 * Return: integer indicating success (zero) or failure (non-zero)
575 */
576 int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
577 void *src, unsigned long nbytes)
578 {
579 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
580 }
581 EXPORT_SYMBOL_GPL(visorbus_write_channel);
582
583 /*
584 * visorbus_enable_channel_interrupts() - enables interrupts on the
585 * designated device
586 * @dev: the device on which to enable interrupts
587 *
588 * Currently we don't yet have a real interrupt, so for now we just call the
589 * interrupt function periodically via a timer.
590 */
591 int visorbus_enable_channel_interrupts(struct visor_device *dev)
592 {
593 struct visor_driver *drv = to_visor_driver(dev->device.driver);
594
595 if (!drv->channel_interrupt) {
596 dev_err(&dev->device, "%s no interrupt function!\n", __func__);
597 return -ENOENT;
598 }
599
600 return dev_start_periodic_work(dev);
601 }
602 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
603
604 /*
605 * visorbus_disable_channel_interrupts() - disables interrupts on the
606 * designated device
607 * @dev: the device on which to disable interrupts
608 */
609 void visorbus_disable_channel_interrupts(struct visor_device *dev)
610 {
611 dev_stop_periodic_work(dev);
612 }
613 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
614
615 /*
616 * create_visor_device() - create visor device as a result of receiving the
617 * controlvm device_create message for a new device
618 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
619 * for chipset_bus_no and chipset_dev_no, that will be initialized
620 *
621 * This is how everything starts from the device end.
622 * This function is called when a channel first appears via a ControlVM
623 * message. In response, this function allocates a visor_device to
624 * correspond to the new channel, and attempts to connect it the appropriate
625 * driver. If the appropriate driver is found, the visor_driver.probe()
626 * function for that driver will be called, and will be passed the new
627 * visor_device that we just created.
628 *
629 * It's ok if the appropriate driver is not yet loaded, because in that case
630 * the new device struct will just stick around in the bus' list of devices.
631 * When the appropriate driver calls visorbus_register_visor_driver(), the
632 * visor_driver.probe() for the new driver will be called with the new
633 * device.
634 *
635 * Return: 0 if successful, otherwise the negative value returned by
636 * device_add() indicating the reason for failure
637 */
638 int create_visor_device(struct visor_device *dev)
639 {
640 int err;
641 u32 chipset_bus_no = dev->chipset_bus_no;
642 u32 chipset_dev_no = dev->chipset_dev_no;
643
644 mutex_init(&dev->visordriver_callback_lock);
645 dev->device.bus = &visorbus_type;
646 dev->device.groups = channel_groups;
647 device_initialize(&dev->device);
648 dev->device.release = visorbus_release_device;
649 /* keep a reference just for us (now 2) */
650 get_device(&dev->device);
651 setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev);
652
653 /*
654 * bus_id must be a unique name with respect to this bus TYPE
655 * (NOT bus instance). That's why we need to include the bus
656 * number within the name.
657 */
658 err = dev_set_name(&dev->device, "vbus%u:dev%u",
659 chipset_bus_no, chipset_dev_no);
660 if (err)
661 goto err_put;
662
663 /*
664 * device_add does this:
665 * bus_add_device(dev)
666 * ->device_attach(dev)
667 * ->for each driver drv registered on the bus that dev is on
668 * if (dev.drv) ** device already has a driver **
669 * ** not sure we could ever get here... **
670 * else
671 * if (bus.match(dev,drv)) [visorbus_match]
672 * dev.drv = drv
673 * if (!drv.probe(dev)) [visordriver_probe_device]
674 * dev.drv = NULL
675 *
676 * Note that device_add does NOT fail if no driver failed to
677 * claim the device. The device will be linked onto
678 * bus_type.klist_devices regardless (use bus_for_each_dev).
679 */
680 err = device_add(&dev->device);
681 if (err < 0)
682 goto err_put;
683
684 list_add_tail(&dev->list_all, &list_all_device_instances);
685 dev->state.created = 1;
686 visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
687 /* success: reference kept via unmatched get_device() */
688 return 0;
689
690 err_put:
691 put_device(&dev->device);
692 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
693 return err;
694 }
695
696 void remove_visor_device(struct visor_device *dev)
697 {
698 list_del(&dev->list_all);
699 put_device(&dev->device);
700 device_unregister(&dev->device);
701 visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
702 }
703
704 static int get_vbus_header_info(struct visorchannel *chan,
705 struct visor_vbus_headerinfo *hdr_info)
706 {
707 int err;
708
709 if (!visor_check_channel(visorchannel_get_header(chan),
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 -ENODEV;
973
974 if (!drv->remove)
975 return -ENODEV;
976
977 if (!drv->pause)
978 return -ENODEV;
979
980 if (!drv->resume)
981 return -ENODEV;
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, 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 drv = to_visor_driver(dev->device.driver);
1162 if (!drv)
1163 return -ENODEV;
1164
1165 if (dev->pausing || dev->resuming)
1166 return -EBUSY;
1167
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 }