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