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