]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/staging/unisys/visorbus/visorbus_main.c
treewide: setup_timer() -> timer_setup()
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / unisys / visorbus / visorbus_main.c
CommitLineData
060b293b 1/*
6f14cc18 2 * Copyright � 2010 - 2015 UNISYS CORPORATION
3703987c
EA
3 * All rights reserved.
4 *
6f14cc18
BR
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.
3703987c
EA
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
b71d87b7 16#include <linux/ctype.h>
8217becc 17#include <linux/debugfs.h>
eb6eb1e1
DK
18#include <linux/module.h>
19#include <linux/slab.h>
3703987c
EA
20#include <linux/uuid.h>
21
4b78000e 22#include "visorbus.h"
c79b28f7 23#include "visorbus_private.h"
4b78000e 24
b32c5cb8
AS
25static const guid_t visor_vbus_channel_guid = VISOR_VBUS_CHANNEL_GUID;
26
060b293b 27/* Display string that is guaranteed to be no longer the 99 characters */
38d56c2f 28#define LINESIZE 99
ffcdb101 29#define POLLJIFFIES_NORMALCHANNEL 10
3703987c 30
060b293b
SW
31/* stores whether bus_registration was successful */
32static bool initialized;
8217becc 33static struct dentry *visorbus_debugfs_dir;
6155a3cf 34
59fd2c8b
PB
35/*
36 * DEVICE type attributes
37 *
38 * The modalias file will contain the guid of the device.
39 */
40static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
41 char *buf)
42{
43 struct visor_device *vdev;
b32c5cb8 44 const guid_t *guid;
59fd2c8b
PB
45
46 vdev = to_visor_device(dev);
b32c5cb8
AS
47 guid = visorchannel_get_guid(vdev->visorchannel);
48 return sprintf(buf, "visorbus:%pUl\n", guid);
59fd2c8b
PB
49}
50static DEVICE_ATTR_RO(modalias);
51
52static struct attribute *visorbus_dev_attrs[] = {
53 &dev_attr_modalias.attr,
54 NULL,
55};
56
aa5ebdeb 57ATTRIBUTE_GROUPS(visorbus_dev);
59fd2c8b 58
3703987c 59/* filled in with info about parent chipset driver when we register with it */
55c71eba 60static struct visor_vbus_deviceinfo chipset_driverinfo;
3703987c 61/* filled in with info about this driver, wrt it servicing client busses */
55c71eba 62static struct visor_vbus_deviceinfo clientbus_driverinfo;
3703987c 63
3fd1b3b6 64/* list of visor_device structs, linked via .list_all */
3703987c 65static LIST_HEAD(list_all_bus_instances);
3fd1b3b6 66/* list of visor_device structs, linked via .list_all */
3703987c
EA
67static LIST_HEAD(list_all_device_instances);
68
403043c4
SW
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 */
5f50a34a
DK
75int 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,
403043c4
SW
78 u64 expected_signature)
79{
b32c5cb8 80 if (!guid_is_null(expected_guid)) {
403043c4 81 /* caller wants us to verify type GUID */
b32c5cb8 82 if (!guid_equal(&ch->chtype, expected_guid)) {
e25201d6
SW
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);
403043c4
SW
86 return 0;
87 }
88 }
89 /* verify channel size */
90 if (expected_min_bytes > 0) {
91 if (ch->size < expected_min_bytes) {
e25201d6
SW
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);
403043c4
SW
96 return 0;
97 }
98 }
99 /* verify channel version */
100 if (expected_version > 0) {
101 if (ch->version_id != expected_version) {
e25201d6
SW
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);
403043c4
SW
106 return 0;
107 }
108 }
109 /* verify channel signature */
110 if (expected_signature > 0) {
111 if (ch->signature != expected_signature) {
e25201d6
SW
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);
403043c4
SW
115 return 0;
116 }
117 }
118 return 1;
119}
403043c4 120
baf9b705 121static int visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
3703987c 122{
59fd2c8b 123 struct visor_device *dev;
b32c5cb8 124 const guid_t *guid;
59fd2c8b
PB
125
126 dev = to_visor_device(xdev);
b32c5cb8 127 guid = visorchannel_get_guid(dev->visorchannel);
b32c5cb8 128 return add_uevent_var(env, "MODALIAS=visorbus:%pUl", guid);
3703987c
EA
129}
130
b84be59a 131/*
3fd1b3b6
DB
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
3703987c 139 */
baf9b705 140static int visorbus_match(struct device *xdev, struct device_driver *xdrv)
3703987c 141{
b32c5cb8 142 const guid_t *channel_type;
3703987c
EA
143 int i;
144 struct visor_device *dev;
145 struct visor_driver *drv;
cb3b5dcc 146 struct visorchannel *chan;
3703987c
EA
147
148 dev = to_visor_device(xdev);
b32c5cb8 149 channel_type = visorchannel_get_guid(dev->visorchannel);
d4e8a22e 150 drv = to_visor_driver(xdrv);
cb3b5dcc 151 chan = dev->visorchannel;
3703987c 152 if (!drv->channel_types)
8e33f48c 153 return 0;
425eaf47 154 for (i = 0; !guid_is_null(&drv->channel_types[i].guid); i++)
cb3b5dcc
SW
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))
8e33f48c 163 return i + 1;
8e33f48c 164 return 0;
3703987c
EA
165}
166
7993b40c
DK
167/*
168 * This describes the TYPE of bus.
060b293b 169 * (Don't confuse this with an INSTANCE of the bus.)
7993b40c 170 */
69a4d1e7 171static struct bus_type visorbus_type = {
7993b40c
DK
172 .name = "visorbus",
173 .match = visorbus_match,
174 .uevent = visorbus_uevent,
175 .dev_groups = visorbus_dev_groups,
7993b40c
DK
176};
177
69a4d1e7
DK
178struct visor_busdev {
179 u32 bus_no;
180 u32 dev_no;
181};
182
183static 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
614b083d
DK
188 if (vdev->chipset_bus_no == id->bus_no &&
189 vdev->chipset_dev_no == id->dev_no)
69a4d1e7
DK
190 return 1;
191 return 0;
192}
193
194struct 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
b84be59a 213/*
ce4617ef
DB
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
3fd1b3b6 217 * @xdev: struct device for the bus being released
3703987c 218 */
baf9b705 219static void visorbus_release_busdevice(struct device *xdev)
3703987c 220{
343506bf 221 struct visor_device *dev = dev_get_drvdata(xdev);
3703987c 222
1c218004 223 debugfs_remove(dev->debugfs_bus_info);
8217becc 224 debugfs_remove_recursive(dev->debugfs_dir);
ae54a287 225 visorchannel_destroy(dev->visorchannel);
343506bf 226 kfree(dev);
3703987c
EA
227}
228
b84be59a 229/*
3fd1b3b6
DB
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
3703987c 233 */
baf9b705 234static void visorbus_release_device(struct device *xdev)
3703987c
EA
235{
236 struct visor_device *dev = to_visor_device(xdev);
237
d72e1a1c 238 visorchannel_destroy(dev->visorchannel);
3703987c
EA
239 kfree(dev);
240}
241
3fd1b3b6 242/*
7915a3c4 243 * BUS specific channel attributes to appear under
3fd1b3b6
DB
244 * /sys/bus/visorbus<x>/dev<y>/channel
245 */
246
79573162
DZ
247static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
248 char *buf)
826b6a0f 249{
79573162
DZ
250 struct visor_device *vdev = to_visor_device(dev);
251
7a41385e
DB
252 return sprintf(buf, "0x%llx\n",
253 visorchannel_get_physaddr(vdev->visorchannel));
826b6a0f 254}
a25280b3 255static DEVICE_ATTR_RO(physaddr);
826b6a0f 256
79573162
DZ
257static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
826b6a0f 259{
79573162
DZ
260 struct visor_device *vdev = to_visor_device(dev);
261
7a41385e 262 return sprintf(buf, "0x%lx\n",
da56cb04 263 visorchannel_get_nbytes(vdev->visorchannel));
826b6a0f 264}
a25280b3 265static DEVICE_ATTR_RO(nbytes);
826b6a0f 266
79573162
DZ
267static 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
7a41385e
DB
272 return sprintf(buf, "0x%llx\n",
273 visorchannel_get_clientpartition(vdev->visorchannel));
826b6a0f 274}
a25280b3 275static DEVICE_ATTR_RO(clientpartition);
826b6a0f 276
79573162
DZ
277static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
278 char *buf)
826b6a0f 279{
79573162 280 struct visor_device *vdev = to_visor_device(dev);
38d56c2f 281 char typeid[LINESIZE];
826b6a0f 282
7a41385e
DB
283 return sprintf(buf, "%s\n",
284 visorchannel_id(vdev->visorchannel, typeid));
826b6a0f 285}
a25280b3 286static DEVICE_ATTR_RO(typeguid);
826b6a0f 287
79573162
DZ
288static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
289 char *buf)
826b6a0f 290{
79573162 291 struct visor_device *vdev = to_visor_device(dev);
38d56c2f 292 char zoneid[LINESIZE];
826b6a0f 293
7a41385e
DB
294 return sprintf(buf, "%s\n",
295 visorchannel_zoneid(vdev->visorchannel, zoneid));
826b6a0f 296}
a25280b3 297static DEVICE_ATTR_RO(zoneguid);
826b6a0f 298
79573162
DZ
299static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
300 char *buf)
826b6a0f
PB
301{
302 int i = 0;
79573162
DZ
303 struct bus_type *xbus = dev->bus;
304 struct device_driver *xdrv = dev->driver;
826b6a0f
PB
305 struct visor_driver *drv = NULL;
306
a3276bf3 307 if (!xdrv)
826b6a0f 308 return 0;
79573162 309 i = xbus->match(dev, xdrv);
826b6a0f
PB
310 if (!i)
311 return 0;
312 drv = to_visor_driver(xdrv);
7a41385e 313 return sprintf(buf, "%s\n", drv->channel_types[i - 1].name);
826b6a0f 314}
79573162 315static DEVICE_ATTR_RO(typename);
79573162
DZ
316
317static struct attribute *channel_attrs[] = {
b372fee1
ZD
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
826b6a0f
PB
325};
326
aa5ebdeb 327ATTRIBUTE_GROUPS(channel);
826b6a0f 328
3fd1b3b6
DB
329/*
330 * BUS instance attributes
3703987c
EA
331 *
332 * define & implement display of bus attributes under
3fd1b3b6 333 * /sys/bus/visorbus/devices/visorbus<n>.
3703987c 334 */
d181dd03 335static ssize_t partition_handle_show(struct device *dev,
5f50a34a 336 struct device_attribute *attr, char *buf)
7b9de71b 337{
5ecbd5d4
DZ
338 struct visor_device *vdev = to_visor_device(dev);
339 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
3703987c 340
7a41385e 341 return sprintf(buf, "0x%llx\n", handle);
3703987c 342}
a25280b3 343static DEVICE_ATTR_RO(partition_handle);
3703987c 344
d181dd03 345static ssize_t partition_guid_show(struct device *dev,
5f50a34a 346 struct device_attribute *attr, char *buf)
7b9de71b 347{
5ecbd5d4 348 struct visor_device *vdev = to_visor_device(dev);
3703987c 349
b32c5cb8 350 return sprintf(buf, "{%pUb}\n", &vdev->partition_guid);
3703987c 351}
a25280b3 352static DEVICE_ATTR_RO(partition_guid);
3703987c 353
d181dd03 354static ssize_t partition_name_show(struct device *dev,
5f50a34a 355 struct device_attribute *attr, char *buf)
7b9de71b 356{
5ecbd5d4 357 struct visor_device *vdev = to_visor_device(dev);
3703987c 358
7a41385e 359 return sprintf(buf, "%s\n", vdev->name);
3703987c 360}
a25280b3 361static DEVICE_ATTR_RO(partition_name);
3703987c 362
d181dd03 363static ssize_t channel_addr_show(struct device *dev,
5f50a34a 364 struct device_attribute *attr, char *buf)
7b9de71b 365{
5ecbd5d4
DZ
366 struct visor_device *vdev = to_visor_device(dev);
367 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
3703987c 368
7a41385e 369 return sprintf(buf, "0x%llx\n", addr);
3703987c 370}
a25280b3 371static DEVICE_ATTR_RO(channel_addr);
3703987c 372
d181dd03 373static ssize_t channel_bytes_show(struct device *dev,
5f50a34a 374 struct device_attribute *attr, char *buf)
7b9de71b 375{
5ecbd5d4
DZ
376 struct visor_device *vdev = to_visor_device(dev);
377 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
3703987c 378
7a41385e 379 return sprintf(buf, "0x%llx\n", nbytes);
3703987c 380}
a25280b3 381static DEVICE_ATTR_RO(channel_bytes);
3703987c 382
d181dd03 383static ssize_t channel_id_show(struct device *dev,
5f50a34a 384 struct device_attribute *attr, char *buf)
7b9de71b 385{
5ecbd5d4 386 struct visor_device *vdev = to_visor_device(dev);
3703987c
EA
387 int len = 0;
388
bf8d0e94
DB
389 visorchannel_id(vdev->visorchannel, buf);
390 len = strlen(buf);
391 buf[len++] = '\n';
3703987c
EA
392 return len;
393}
a25280b3 394static DEVICE_ATTR_RO(channel_id);
3703987c 395
aa5ebdeb 396static struct attribute *visorbus_attrs[] = {
b372fee1
ZD
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
d181dd03 404};
3703987c 405
aa5ebdeb 406ATTRIBUTE_GROUPS(visorbus);
3703987c 407
8217becc
TS
408/*
409 * BUS debugfs entries
410 *
411 * define & implement display of debugfs attributes under
412 * /sys/kernel/debug/visorbus/visorbus<n>.
413 */
d4e8a22e 414
738561a8 415/*
55c71eba 416 * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo
738561a8 417 * and write it to a seq_file
55c71eba 418 * @devinfo: the struct visor_vbus_deviceinfo to format
738561a8
DK
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 */
baf9b705
SW
425static void vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo,
426 struct seq_file *seq, int devix)
738561a8 427{
060b293b 428 /* uninitialized vbus device entry */
738561a8 429 if (!isprint(devinfo->devtype[0]))
060b293b 430 return;
738561a8
DK
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, " ");
738561a8
DK
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}
8217becc 447
1c218004 448static int bus_info_debugfs_show(struct seq_file *seq, void *v)
8217becc 449{
d4e8a22e 450 int i = 0;
8217becc 451 unsigned long off;
55c71eba 452 struct visor_vbus_deviceinfo dev_info;
d4e8a22e
DB
453 struct visor_device *vdev = seq->private;
454 struct visorchannel *channel = vdev->visorchannel;
8217becc
TS
455
456 if (!channel)
457 return 0;
458
459 seq_printf(seq,
453ca193 460 "Client device/driver info for %s partition (vbus #%u):\n",
8217becc
TS
461 ((vdev->name) ? (char *)(vdev->name) : ""),
462 vdev->chipset_bus_no);
463 if (visorchannel_read(channel,
55c71eba 464 offsetof(struct visor_vbus_channel, chp_info),
8217becc
TS
465 &dev_info, sizeof(dev_info)) >= 0)
466 vbuschannel_print_devinfo(&dev_info, seq, -1);
467 if (visorchannel_read(channel,
55c71eba 468 offsetof(struct visor_vbus_channel, bus_info),
8217becc
TS
469 &dev_info, sizeof(dev_info)) >= 0)
470 vbuschannel_print_devinfo(&dev_info, seq, -1);
d4e8a22e 471
55c71eba 472 off = offsetof(struct visor_vbus_channel, dev_info);
8217becc
TS
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 }
8217becc
TS
480 return 0;
481}
482
1c218004 483static int bus_info_debugfs_open(struct inode *inode, struct file *file)
8217becc 484{
1c218004 485 return single_open(file, bus_info_debugfs_show, inode->i_private);
8217becc
TS
486}
487
1c218004 488static const struct file_operations bus_info_debugfs_fops = {
8217becc 489 .owner = THIS_MODULE,
1c218004 490 .open = bus_info_debugfs_open,
8217becc
TS
491 .read = seq_read,
492 .llseek = seq_lseek,
493 .release = single_release,
494};
495
e99e88a9 496static void dev_periodic_work(struct timer_list *t)
3703987c 497{
e99e88a9 498 struct visor_device *dev = from_timer(dev, t, timer);
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;
4e95347b 509
3703987c
EA
510 /* now up by at least 2 */
511 get_device(&dev->device);
9ebab649
TS
512 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
513 add_timer(&dev->timer);
514 dev->timer_active = true;
b90194d9 515 return 0;
3703987c
EA
516}
517
baf9b705 518static void dev_stop_periodic_work(struct visor_device *dev)
3703987c 519{
9ebab649
TS
520 if (!dev->timer_active)
521 return;
4e95347b 522
9ebab649
TS
523 del_timer_sync(&dev->timer);
524 dev->timer_active = false;
525 put_device(&dev->device);
3703987c
EA
526}
527
b84be59a 528/*
3fd1b3b6
DB
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
3703987c 537 */
baf9b705 538static int visordriver_remove_device(struct device *xdev)
3703987c 539{
7f91228d
DK
540 struct visor_device *dev = to_visor_device(xdev);
541 struct visor_driver *drv = to_visor_driver(xdev->driver);
d4e8a22e 542
d505855e 543 mutex_lock(&dev->visordriver_callback_lock);
779d0752 544 dev->being_removed = true;
c2d00b21 545 drv->remove(dev);
d505855e 546 mutex_unlock(&dev->visordriver_callback_lock);
d4e8a22e 547 dev_stop_periodic_work(dev);
3703987c 548 put_device(&dev->device);
df7f46e8 549 return 0;
3703987c
EA
550}
551
060b293b 552/*
3fd1b3b6
DB
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.
3703987c 558 */
baf9b705 559void visorbus_unregister_visor_driver(struct visor_driver *drv)
3703987c 560{
3703987c
EA
561 driver_unregister(&drv->driver);
562}
563EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
564
060b293b 565/*
3fd1b3b6
DB
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 *
7915a3c4 573 * If receiving a message, use the visorchannel_signalremove() function instead.
3fd1b3b6
DB
574 *
575 * Return: integer indicating success (zero) or failure (non-zero)
576 */
baf9b705
SW
577int visorbus_read_channel(struct visor_device *dev, unsigned long offset,
578 void *dest, unsigned long nbytes)
3703987c
EA
579{
580 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
581}
582EXPORT_SYMBOL_GPL(visorbus_read_channel);
583
060b293b 584/*
3fd1b3b6
DB
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 *
7915a3c4 592 * If sending a message, use the visorchannel_signalinsert() function instead.
3fd1b3b6
DB
593 *
594 * Return: integer indicating success (zero) or failure (non-zero)
595 */
baf9b705
SW
596int visorbus_write_channel(struct visor_device *dev, unsigned long offset,
597 void *src, unsigned long nbytes)
3703987c
EA
598{
599 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
600}
601EXPORT_SYMBOL_GPL(visorbus_write_channel);
602
060b293b 603/*
3fd1b3b6
DB
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.
3703987c 610 */
baf9b705 611int visorbus_enable_channel_interrupts(struct visor_device *dev)
3703987c 612{
396e36c9
TS
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__);
5dca9b29 617 return -ENOENT;
396e36c9
TS
618 }
619
5dca9b29 620 return dev_start_periodic_work(dev);
3703987c
EA
621}
622EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
623
060b293b 624/*
3fd1b3b6
DB
625 * visorbus_disable_channel_interrupts() - disables interrupts on the
626 * designated device
627 * @dev: the device on which to disable interrupts
628 */
baf9b705 629void visorbus_disable_channel_interrupts(struct visor_device *dev)
3703987c
EA
630{
631 dev_stop_periodic_work(dev);
632}
633EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
634
b84be59a 635/*
3fd1b3b6
DB
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
7915a3c4
DK
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.
3703987c 648 *
3fd1b3b6
DB
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
7915a3c4 652 * visor_driver.probe() for the new driver will be called with the new device.
3fd1b3b6
DB
653 *
654 * Return: 0 if successful, otherwise the negative value returned by
655 * device_add() indicating the reason for failure
3703987c 656 */
51c0f81c 657int create_visor_device(struct visor_device *dev)
3703987c 658{
0b2acf34 659 int err;
a298bc0b
DZ
660 u32 chipset_bus_no = dev->chipset_bus_no;
661 u32 chipset_dev_no = dev->chipset_dev_no;
3703987c 662
d505855e 663 mutex_init(&dev->visordriver_callback_lock);
3703987c 664 dev->device.bus = &visorbus_type;
aa5ebdeb 665 dev->device.groups = channel_groups;
3703987c
EA
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);
e99e88a9 670 timer_setup(&dev->timer, dev_periodic_work, 0);
3fd1b3b6 671 /*
7915a3c4
DK
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.
3703987c 675 */
e0d210ae
DK
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;
3fd1b3b6
DB
680 /*
681 * device_add does this:
3703987c
EA
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 *
7915a3c4
DK
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).
3703987c 696 */
0b2acf34 697 err = device_add(&dev->device);
ebdc1b88 698 if (err < 0)
0b2acf34 699 goto err_put;
a298bc0b 700 list_add_tail(&dev->list_all, &list_all_device_instances);
51c0f81c
SW
701 dev->state.created = 1;
702 visorbus_response(dev, err, CONTROLVM_DEVICE_CREATE);
060b293b
SW
703 /* success: reference kept via unmatched get_device() */
704 return 0;
3703987c 705
0b2acf34 706err_put:
a298bc0b 707 put_device(&dev->device);
ebdc1b88 708 dev_err(&dev->device, "Creating visor device failed. %d\n", err);
0b2acf34 709 return err;
3703987c
EA
710}
711
b74856b4 712void remove_visor_device(struct visor_device *dev)
3703987c
EA
713{
714 list_del(&dev->list_all);
3703987c 715 put_device(&dev->device);
fd9e450c
DK
716 if (dev->pending_msg_hdr)
717 visorbus_response(dev, 0, CONTROLVM_DEVICE_DESTROY);
3703987c
EA
718 device_unregister(&dev->device);
719}
720
baf9b705 721static int get_vbus_header_info(struct visorchannel *chan,
e25201d6 722 struct device *dev,
baf9b705 723 struct visor_vbus_headerinfo *hdr_info)
3703987c 724{
711c67f4
DK
725 int err;
726
315dfc84 727 if (!visor_check_channel(visorchannel_get_header(chan),
e25201d6 728 dev,
b32c5cb8 729 &visor_vbus_channel_guid,
315dfc84
SW
730 "vbus",
731 sizeof(struct visor_vbus_channel),
732 VISOR_VBUS_CHANNEL_VERSIONID,
e9b9275c 733 VISOR_CHANNEL_SIGNATURE))
f748f64f
BR
734 return -EINVAL;
735
711c67f4
DK
736 err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
737 sizeof(*hdr_info));
738 if (err < 0)
739 return err;
55c71eba 740 if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo))
f748f64f 741 return -EINVAL;
3703987c 742 if (hdr_info->device_info_struct_bytes <
55c71eba 743 sizeof(struct visor_vbus_deviceinfo))
f748f64f 744 return -EINVAL;
f748f64f 745 return 0;
3703987c
EA
746}
747
b84be59a 748/*
3fd1b3b6 749 * write_vbus_chp_info() - write the contents of <info> to the struct
55c71eba 750 * visor_vbus_channel.chp_info
3fd1b3b6
DB
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.
ff13cf37 757 *
3fd1b3b6 758 * Returns no value since this is debug information and not needed for
ff13cf37 759 * device functionality.
48117895 760 */
baf9b705
SW
761static void write_vbus_chp_info(struct visorchannel *chan,
762 struct visor_vbus_headerinfo *hdr_info,
763 struct visor_vbus_deviceinfo *info)
3703987c 764{
5d1a7fd7 765 int off;
3703987c
EA
766
767 if (hdr_info->chp_info_offset == 0)
ff13cf37 768 return;
3703987c 769
5d1a7fd7 770 off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
ff13cf37 771 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
772}
773
b84be59a 774/*
3fd1b3b6 775 * write_vbus_bus_info() - write the contents of <info> to the struct
55c71eba 776 * visor_vbus_channel.bus_info
3fd1b3b6
DB
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.
ff13cf37 783 *
3fd1b3b6 784 * Returns no value since this is debug information and not needed for
ff13cf37 785 * device functionality.
48117895 786 */
baf9b705
SW
787static void write_vbus_bus_info(struct visorchannel *chan,
788 struct visor_vbus_headerinfo *hdr_info,
789 struct visor_vbus_deviceinfo *info)
3703987c 790{
5d1a7fd7 791 int off;
3703987c
EA
792
793 if (hdr_info->bus_info_offset == 0)
ff13cf37 794 return;
4e95347b 795
5d1a7fd7 796 off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
ff13cf37 797 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
798}
799
b84be59a 800/*
3fd1b3b6 801 * write_vbus_dev_info() - write the contents of <info> to the struct
55c71eba 802 * visor_vbus_channel.dev_info[<devix>]
3fd1b3b6
DB
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
ff13cf37 807 *
3fd1b3b6
DB
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
ff13cf37 812 * device functionality.
3703987c 813 */
baf9b705
SW
814static 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)
3703987c 818{
5d1a7fd7 819 int off;
3703987c
EA
820
821 if (hdr_info->dev_info_offset == 0)
ff13cf37 822 return;
5d1a7fd7
DK
823 off = (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
824 (hdr_info->device_info_struct_bytes * devix);
ff13cf37 825 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
826}
827
b5577d79 828static void bus_device_info_init(
55c71eba 829 struct visor_vbus_deviceinfo *bus_device_info_ptr,
b5577d79
DK
830 const char *dev_type, const char *drv_name)
831{
55c71eba 832 memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo));
b5577d79
DK
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
b84be59a 844/*
e3b2ed66
EA
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
3fd1b3b6 849 * @visordev: struct visor_device for the desired device
3703987c 850 */
e3b2ed66 851static void publish_vbus_dev_info(struct visor_device *visordev)
3703987c
EA
852{
853 int i;
d32517e3 854 struct visor_device *bdev;
3703987c 855 struct visor_driver *visordrv;
f7a34ff7
TS
856 u32 bus_no = visordev->chipset_bus_no;
857 u32 dev_no = visordev->chipset_dev_no;
55c71eba 858 struct visor_vbus_deviceinfo dev_info;
3703987c 859 const char *chan_type_name = NULL;
55c71eba 860 struct visor_vbus_headerinfo *hdr_info;
3703987c
EA
861
862 if (!visordev->device.driver)
216c3e2c 863 return;
d32517e3
DZ
864 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
865 if (!bdev)
866 return;
55c71eba 867 hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info;
5990b39e
TS
868 if (!hdr_info)
869 return;
d32517e3 870 visordrv = to_visor_driver(visordev->device.driver);
3703987c 871
3fd1b3b6
DB
872 /*
873 * Within the list of device types (by GUID) that the driver
3703987c
EA
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++) {
5d48942c
DK
879 if (guid_equal(&visordrv->channel_types[i].guid,
880 &visordev->channel_type_guid)) {
3703987c
EA
881 chan_type_name = visordrv->channel_types[i].name;
882 break;
883 }
884 }
e82ed633 885 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
d32517e3 886 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
d32517e3
DZ
887 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
888 write_vbus_bus_info(bdev->visorchannel, hdr_info,
889 &clientbus_driverinfo);
3703987c
EA
890}
891
b84be59a 892/*
7a0ee694
DK
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 */
baf9b705 907static int visordriver_probe_device(struct device *xdev)
7a0ee694 908{
61f38f9a 909 int err;
7f91228d
DK
910 struct visor_driver *drv = to_visor_driver(xdev->driver);
911 struct visor_device *dev = to_visor_device(xdev);
7a0ee694
DK
912
913 mutex_lock(&dev->visordriver_callback_lock);
914 dev->being_removed = false;
61f38f9a
DK
915 err = drv->probe(dev);
916 if (err) {
917 mutex_unlock(&dev->visordriver_callback_lock);
918 return err;
7a0ee694 919 }
61f38f9a
DK
920 /* success: reference kept via unmatched get_device() */
921 get_device(&dev->device);
922 publish_vbus_dev_info(dev);
7a0ee694 923 mutex_unlock(&dev->visordriver_callback_lock);
61f38f9a 924 return 0;
7a0ee694
DK
925}
926
060b293b 927/*
7915a3c4
DK
928 * visorbus_register_visor_driver() - registers the provided visor driver for
929 * handling one or more visor device
7a0ee694
DK
930 * types (channel_types)
931 * @drv: the driver to register
932 *
7915a3c4
DK
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:
7a0ee694
DK
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 */
975int visorbus_register_visor_driver(struct visor_driver *drv)
976{
060b293b 977 /* can't register on a nonexistent bus */
b0512faf 978 if (!initialized)
060b293b 979 return -ENODEV;
c2d00b21 980 if (!drv->probe)
3459e83a 981 return -EINVAL;
c2d00b21 982 if (!drv->remove)
3459e83a 983 return -EINVAL;
c2d00b21 984 if (!drv->pause)
3459e83a 985 return -EINVAL;
c2d00b21 986 if (!drv->resume)
3459e83a 987 return -EINVAL;
c2d00b21 988
7a0ee694
DK
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;
7a0ee694
DK
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 */
293deb2c 1006 return driver_register(&drv->driver);
7a0ee694
DK
1007}
1008EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
1009
b84be59a 1010/*
9e78fd35 1011 * visorbus_create_instance() - create a device instance for the visorbus itself
3fd1b3b6
DB
1012 * @dev: struct visor_device indicating the bus instance
1013 *
1014 * Return: 0 for success, otherwise negative errno value indicating reason for
1015 * failure
3703987c 1016 */
fdf5b9ac 1017int visorbus_create_instance(struct visor_device *dev)
3703987c 1018{
d32517e3 1019 int id = dev->chipset_bus_no;
8217becc 1020 int err;
55c71eba 1021 struct visor_vbus_headerinfo *hdr_info;
3703987c 1022
7726f813 1023 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
c2c667d6
BR
1024 if (!hdr_info)
1025 return -ENOMEM;
343506bf
DZ
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;
8217becc
TS
1030 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
1031 visorbus_debugfs_dir);
1c218004
DK
1032 dev->debugfs_bus_info = debugfs_create_file("client_bus_info", 0440,
1033 dev->debugfs_dir, dev,
1034 &bus_info_debugfs_fops);
362f87f3 1035 dev_set_drvdata(&dev->device, dev);
e25201d6 1036 err = get_vbus_header_info(dev->visorchannel, &dev->device, hdr_info);
362f87f3 1037 if (err < 0)
8217becc 1038 goto err_debugfs_dir;
362f87f3 1039 err = device_register(&dev->device);
450333f1 1040 if (err < 0)
362f87f3 1041 goto err_debugfs_dir;
343506bf 1042 list_add_tail(&dev->list_all, &list_all_bus_instances);
fdf5b9ac 1043 dev->state.created = 1;
362f87f3 1044 dev->vbus_hdr_info = (void *)hdr_info;
9f1d28fa
DK
1045 write_vbus_chp_info(dev->visorchannel, hdr_info, &chipset_driverinfo);
1046 write_vbus_bus_info(dev->visorchannel, hdr_info, &clientbus_driverinfo);
fdf5b9ac 1047 visorbus_response(dev, err, CONTROLVM_BUS_CREATE);
362f87f3 1048 return 0;
8217becc
TS
1049
1050err_debugfs_dir:
1051 debugfs_remove_recursive(dev->debugfs_dir);
8217becc 1052 kfree(hdr_info);
23d01c42 1053 dev_err(&dev->device, "%s failed: %d\n", __func__, err);
8217becc 1054 return err;
3703987c
EA
1055}
1056
b84be59a 1057/*
9e78fd35 1058 * visorbus_remove_instance() - remove a device instance for the visorbus itself
3fd1b3b6 1059 * @dev: struct visor_device indentifying the bus to remove
3703987c 1060 */
a7093ba1 1061void visorbus_remove_instance(struct visor_device *dev)
3703987c 1062{
3fd1b3b6
DB
1063 /*
1064 * Note that this will result in the release method for
343506bf 1065 * dev->dev being called, which will call
3703987c
EA
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 */
343506bf
DZ
1071 kfree(dev->vbus_hdr_info);
1072 list_del(&dev->list_all);
ae54a287
DK
1073 if (dev->pending_msg_hdr)
1074 visorbus_response(dev, 0, CONTROLVM_BUS_DESTROY);
343506bf 1075 device_unregister(&dev->device);
3703987c
EA
1076}
1077
b84be59a 1078/*
9e78fd35 1079 * remove_all_visor_devices() - remove all child visorbus device instances
3703987c 1080 */
baf9b705 1081static void remove_all_visor_devices(void)
3703987c
EA
1082{
1083 struct list_head *listentry, *listtmp;
1084
1085 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
17e4bdda
DK
1086 struct visor_device *dev;
1087
1088 dev = list_entry(listentry, struct visor_device, list_all);
3703987c
EA
1089 remove_visor_device(dev);
1090 }
1091}
1092
b84be59a 1093/*
3fd1b3b6
DB
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
3703987c 1101 */
baf9b705 1102static void pause_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1103{
1104 if (!dev->pausing)
216c3e2c 1105 return;
3703987c 1106
779d0752 1107 dev->pausing = false;
722e73d5
SW
1108 visorbus_device_changestate_response(dev, status,
1109 segment_state_standby);
3703987c
EA
1110}
1111
b84be59a 1112/*
3fd1b3b6
DB
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
3703987c 1120 */
baf9b705 1121static void resume_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1122{
1123 if (!dev->resuming)
216c3e2c 1124 return;
3703987c 1125
779d0752 1126 dev->resuming = false;
3fd1b3b6
DB
1127 /*
1128 * Notify the chipset driver that the resume is complete,
3703987c 1129 * which will presumably want to send some sort of response to
48117895
GL
1130 * the initiator.
1131 */
722e73d5
SW
1132 visorbus_device_changestate_response(dev, status,
1133 segment_state_running);
3703987c
EA
1134}
1135
b84be59a 1136/*
451072e3
SW
1137 * visorchipset_initiate_device_pause_resume() - start a pause or resume
1138 * operation for a visor device
3fd1b3b6
DB
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().
3703987c 1146 */
baf9b705
SW
1147static int visorchipset_initiate_device_pause_resume(struct visor_device *dev,
1148 bool is_pause)
3703987c 1149{
15d9ce9d 1150 int err;
473659df 1151 struct visor_driver *drv;
3703987c 1152
68f4f766
DK
1153 /* If no driver associated with the device nothing to pause/resume */
1154 if (!dev->device.driver)
1155 return 0;
15d9ce9d
DK
1156 if (dev->pausing || dev->resuming)
1157 return -EBUSY;
3703987c 1158
68f4f766 1159 drv = to_visor_driver(dev->device.driver);
3703987c 1160 if (is_pause) {
779d0752 1161 dev->pausing = true;
15d9ce9d 1162 err = drv->pause(dev, pause_state_change_complete);
3703987c 1163 } else {
060b293b
SW
1164 /*
1165 * The vbus_dev_info structure in the channel was been cleared,
1166 * make sure it is valid.
48117895 1167 */
e3b2ed66 1168 publish_vbus_dev_info(dev);
779d0752 1169 dev->resuming = true;
15d9ce9d 1170 err = drv->resume(dev, resume_state_change_complete);
3703987c 1171 }
15d9ce9d 1172 return err;
3703987c
EA
1173}
1174
060b293b 1175/*
c0b44136 1176 * visorchipset_device_pause() - start a pause operation for a visor device
3fd1b3b6
DB
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 */
baf9b705 1183int visorchipset_device_pause(struct visor_device *dev_info)
3703987c 1184{
15d9ce9d
DK
1185 int err;
1186
451072e3 1187 err = visorchipset_initiate_device_pause_resume(dev_info, true);
15d9ce9d
DK
1188 if (err < 0) {
1189 dev_info->pausing = false;
b4a8e6ae 1190 return err;
15d9ce9d 1191 }
b4a8e6ae 1192 return 0;
3703987c
EA
1193}
1194
060b293b 1195/*
c0b44136 1196 * visorchipset_device_resume() - start a resume operation for a visor device
3fd1b3b6
DB
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 */
baf9b705 1203int visorchipset_device_resume(struct visor_device *dev_info)
3703987c 1204{
15d9ce9d
DK
1205 int err;
1206
451072e3 1207 err = visorchipset_initiate_device_pause_resume(dev_info, false);
15d9ce9d 1208 if (err < 0) {
15d9ce9d 1209 dev_info->resuming = false;
b4a8e6ae 1210 return err;
15d9ce9d 1211 }
b4a8e6ae 1212 return 0;
3703987c
EA
1213}
1214
baf9b705 1215int visorbus_init(void)
3703987c 1216{
78af1aef 1217 int err;
3703987c 1218
8217becc 1219 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
e82ed633 1220 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
5b6f9b95 1221 err = bus_register(&visorbus_type);
804aab3b
DK
1222 if (err < 0)
1223 return err;
b0512faf 1224 initialized = true;
e82ed633 1225 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
78af1aef 1226 return 0;
3703987c
EA
1227}
1228
baf9b705 1229void visorbus_exit(void)
3703987c
EA
1230{
1231 struct list_head *listentry, *listtmp;
1232
3703987c 1233 remove_all_visor_devices();
3703987c 1234 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
17e4bdda
DK
1235 struct visor_device *dev;
1236
1237 dev = list_entry(listentry, struct visor_device, list_all);
9e78fd35 1238 visorbus_remove_instance(dev);
3703987c 1239 }
5b6f9b95 1240 bus_unregister(&visorbus_type);
b0512faf 1241 initialized = false;
8217becc 1242 debugfs_remove_recursive(visorbus_debugfs_dir);
3703987c 1243}