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