staging: vt6656: rxtx.c Removed multiple dereferencing
[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
DB
145/**
146 * visorbus_releae_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
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);
9ebab649
TS
626 init_timer(&dev->timer);
627 dev->timer.data = (unsigned long)(dev);
628 dev->timer.function = dev_periodic_work;
3703987c 629
3fd1b3b6
DB
630 /*
631 * bus_id must be a unique name with respect to this bus TYPE
3703987c
EA
632 * (NOT bus instance). That's why we need to include the bus
633 * number within the name.
634 */
b4b598fd 635 dev_set_name(&dev->device, "vbus%u:dev%u",
3703987c
EA
636 chipset_bus_no, chipset_dev_no);
637
3fd1b3b6
DB
638 /*
639 * device_add does this:
3703987c
EA
640 * bus_add_device(dev)
641 * ->device_attach(dev)
642 * ->for each driver drv registered on the bus that dev is on
643 * if (dev.drv) ** device already has a driver **
644 * ** not sure we could ever get here... **
645 * else
646 * if (bus.match(dev,drv)) [visorbus_match]
647 * dev.drv = drv
648 * if (!drv.probe(dev)) [visordriver_probe_device]
649 * dev.drv = NULL
650 *
651 * Note that device_add does NOT fail if no driver failed to
652 * claim the device. The device will be linked onto
653 * bus_type.klist_devices regardless (use bus_for_each_dev).
654 */
0b2acf34
DK
655 err = device_add(&dev->device);
656 if (err < 0) {
6daa8205
BT
657 POSTCODE_LINUX(DEVICE_ADD_PC, 0, chipset_bus_no,
658 DIAG_SEVERITY_ERR);
0b2acf34 659 goto err_put;
3703987c
EA
660 }
661
a298bc0b 662 list_add_tail(&dev->list_all, &list_all_device_instances);
0b2acf34 663 return 0; /* success: reference kept via unmatched get_device() */
3703987c 664
0b2acf34 665err_put:
a298bc0b 666 put_device(&dev->device);
0b2acf34 667 return err;
3703987c
EA
668}
669
670static void
671remove_visor_device(struct visor_device *dev)
672{
673 list_del(&dev->list_all);
3703987c
EA
674 put_device(&dev->device);
675 device_unregister(&dev->device);
676}
677
3703987c
EA
678static int
679get_vbus_header_info(struct visorchannel *chan,
680 struct spar_vbus_headerinfo *hdr_info)
681{
3703987c 682 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
f748f64f
BR
683 return -EINVAL;
684
3703987c
EA
685 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
686 sizeof(*hdr_info)) < 0) {
f748f64f 687 return -EIO;
3703987c
EA
688 }
689 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
f748f64f
BR
690 return -EINVAL;
691
3703987c
EA
692 if (hdr_info->device_info_struct_bytes <
693 sizeof(struct ultra_vbus_deviceinfo)) {
f748f64f 694 return -EINVAL;
3703987c 695 }
f748f64f 696 return 0;
3703987c
EA
697}
698
3fd1b3b6
DB
699/**
700 * write_vbus_chp_info() - write the contents of <info> to the struct
701 * spar_vbus_channel_protocol.chp_info
702 * @chan: indentifies the s-Par channel that will be updated
703 * @hdr_info: used to find appropriate channel offset to write data
704 * @info: contains the information to write
705 *
706 * Writes chipset info into the channel memory to be used for diagnostic
707 * purposes.
ff13cf37 708 *
3fd1b3b6 709 * Returns no value since this is debug information and not needed for
ff13cf37 710 * device functionality.
48117895 711 */
ff13cf37 712static void
3703987c
EA
713write_vbus_chp_info(struct visorchannel *chan,
714 struct spar_vbus_headerinfo *hdr_info,
715 struct ultra_vbus_deviceinfo *info)
716{
717 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
718
719 if (hdr_info->chp_info_offset == 0)
ff13cf37 720 return;
3703987c 721
ff13cf37 722 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
723}
724
3fd1b3b6
DB
725/**
726 * write_vbus_bus_info() - write the contents of <info> to the struct
727 * spar_vbus_channel_protocol.bus_info
728 * @chan: indentifies the s-Par channel that will be updated
729 * @hdr_info: used to find appropriate channel offset to write data
730 * @info: contains the information to write
731 *
732 * Writes bus info into the channel memory to be used for diagnostic
733 * purposes.
ff13cf37 734 *
3fd1b3b6 735 * Returns no value since this is debug information and not needed for
ff13cf37 736 * device functionality.
48117895 737 */
ff13cf37 738static void
3703987c
EA
739write_vbus_bus_info(struct visorchannel *chan,
740 struct spar_vbus_headerinfo *hdr_info,
741 struct ultra_vbus_deviceinfo *info)
742{
743 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
744
745 if (hdr_info->bus_info_offset == 0)
ff13cf37 746 return;
3703987c 747
ff13cf37 748 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
749}
750
3fd1b3b6
DB
751/**
752 * write_vbus_dev_info() - write the contents of <info> to the struct
753 * spar_vbus_channel_protocol.dev_info[<devix>]
754 * @chan: indentifies the s-Par channel that will be updated
755 * @hdr_info: used to find appropriate channel offset to write data
756 * @info: contains the information to write
757 * @devix: the relative device number (0..n-1) of the device on the bus
ff13cf37 758 *
3fd1b3b6
DB
759 * Writes device info into the channel memory to be used for diagnostic
760 * purposes.
761 *
762 * Returns no value since this is debug information and not needed for
ff13cf37 763 * device functionality.
3703987c 764 */
ff13cf37 765static void
3703987c
EA
766write_vbus_dev_info(struct visorchannel *chan,
767 struct spar_vbus_headerinfo *hdr_info,
f7a34ff7 768 struct ultra_vbus_deviceinfo *info, unsigned int devix)
3703987c
EA
769{
770 int off =
771 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
772 (hdr_info->device_info_struct_bytes * devix);
773
774 if (hdr_info->dev_info_offset == 0)
ff13cf37 775 return;
3703987c 776
ff13cf37 777 visorchannel_write(chan, off, info, sizeof(*info));
3703987c
EA
778}
779
3fd1b3b6
DB
780/**
781 * fix_vbus_dev_info() - for a child device just created on a client bus, fill
782 * in information about the driver that is controlling
783 * this device into the the appropriate slot within the
784 * vbus channel of the bus instance
785 * @visordev: struct visor_device for the desired device
3703987c
EA
786 */
787static void
788fix_vbus_dev_info(struct visor_device *visordev)
789{
790 int i;
d32517e3 791 struct visor_device *bdev;
3703987c 792 struct visor_driver *visordrv;
f7a34ff7
TS
793 u32 bus_no = visordev->chipset_bus_no;
794 u32 dev_no = visordev->chipset_dev_no;
3703987c
EA
795 struct ultra_vbus_deviceinfo dev_info;
796 const char *chan_type_name = NULL;
7726f813 797 struct spar_vbus_headerinfo *hdr_info;
3703987c
EA
798
799 if (!visordev->device.driver)
216c3e2c 800 return;
3703987c 801
d32517e3
DZ
802 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
803 if (!bdev)
804 return;
5990b39e
TS
805 hdr_info = (struct spar_vbus_headerinfo *)bdev->vbus_hdr_info;
806 if (!hdr_info)
807 return;
d32517e3 808 visordrv = to_visor_driver(visordev->device.driver);
3703987c 809
3fd1b3b6
DB
810 /*
811 * Within the list of device types (by GUID) that the driver
3703987c
EA
812 * says it supports, find out which one of those types matches
813 * the type of this device, so that we can include the device
814 * type name
815 */
816 for (i = 0; visordrv->channel_types[i].name; i++) {
d5b3f1dc
EA
817 if (memcmp(&visordrv->channel_types[i].guid,
818 &visordev->channel_type_guid,
819 sizeof(visordrv->channel_types[i].guid)) == 0) {
3703987c
EA
820 chan_type_name = visordrv->channel_types[i].name;
821 break;
822 }
823 }
824
e82ed633 825 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
d32517e3 826 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
3703987c 827
3fd1b3b6
DB
828 /*
829 * Re-write bus+chipset info, because it is possible that this
830 * was previously written by our evil counterpart, virtpci.
831 */
d32517e3
DZ
832 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
833 write_vbus_bus_info(bdev->visorchannel, hdr_info,
834 &clientbus_driverinfo);
3703987c
EA
835}
836
7a0ee694
DK
837/**
838 * visordriver_probe_device() - handle new visor device coming online
839 * @xdev: struct device for the visor device being probed
840 *
841 * This is called automatically upon adding a visor_device (device_add), or
842 * adding a visor_driver (visorbus_register_visor_driver), but only after
843 * visorbus_match() has returned 1 to indicate a successful match between
844 * driver and device.
845 *
846 * If successful, a reference to the device will be held onto via get_device().
847 *
848 * Return: 0 if successful, meaning the function driver's probe() function
849 * was successful with this device, otherwise a negative errno
850 * value indicating failure reason
851 */
852static int
853visordriver_probe_device(struct device *xdev)
854{
855 int res;
856 struct visor_driver *drv;
857 struct visor_device *dev;
858
859 drv = to_visor_driver(xdev->driver);
860 dev = to_visor_device(xdev);
861
862 if (!drv->probe)
863 return -ENODEV;
864
865 mutex_lock(&dev->visordriver_callback_lock);
866 dev->being_removed = false;
867
868 res = drv->probe(dev);
869 if (res >= 0) {
870 /* success: reference kept via unmatched get_device() */
871 get_device(&dev->device);
872 fix_vbus_dev_info(dev);
873 }
874
875 mutex_unlock(&dev->visordriver_callback_lock);
876 return res;
877}
878
879/**
880 * visorbus_register_visor_driver() - registers the provided visor driver
881 * for handling one or more visor device
882 * types (channel_types)
883 * @drv: the driver to register
884 *
885 * A visor function driver calls this function to register
886 * the driver. The caller MUST fill in the following fields within the
887 * #drv structure:
888 * name, version, owner, channel_types, probe, remove
889 *
890 * Here's how the whole Linux bus / driver / device model works.
891 *
892 * At system start-up, the visorbus kernel module is loaded, which registers
893 * visorbus_type as a bus type, using bus_register().
894 *
895 * All kernel modules that support particular device types on a
896 * visorbus bus are loaded. Each of these kernel modules calls
897 * visorbus_register_visor_driver() in their init functions, passing a
898 * visor_driver struct. visorbus_register_visor_driver() in turn calls
899 * register_driver(&visor_driver.driver). This .driver member is
900 * initialized with generic methods (like probe), whose sole responsibility
901 * is to act as a broker for the real methods, which are within the
902 * visor_driver struct. (This is the way the subclass behavior is
903 * implemented, since visor_driver is essentially a subclass of the
904 * generic driver.) Whenever a driver_register() happens, core bus code in
905 * the kernel does (see device_attach() in drivers/base/dd.c):
906 *
907 * for each dev associated with the bus (the bus that driver is on) that
908 * does not yet have a driver
909 * if bus.match(dev,newdriver) == yes_matched ** .match specified
910 * ** during bus_register().
911 * newdriver.probe(dev) ** for visor drivers, this will call
912 * ** the generic driver.probe implemented in visorbus.c,
913 * ** which in turn calls the probe specified within the
914 * ** struct visor_driver (which was specified by the
915 * ** actual device driver as part of
916 * ** visorbus_register_visor_driver()).
917 *
918 * The above dance also happens when a new device appears.
919 * So the question is, how are devices created within the system?
920 * Basically, just call device_add(dev). See pci_bus_add_devices().
921 * pci_scan_device() shows an example of how to build a device struct. It
922 * returns the newly-created struct to pci_scan_single_device(), who adds it
923 * to the list of devices at PCIBUS.devices. That list of devices is what
924 * is traversed by pci_bus_add_devices().
925 *
926 * Return: integer indicating success (zero) or failure (non-zero)
927 */
928int visorbus_register_visor_driver(struct visor_driver *drv)
929{
930 int rc = 0;
931
932 if (busreg_rc < 0)
933 return -ENODEV; /*can't register on a nonexistent bus*/
934
935 drv->driver.name = drv->name;
936 drv->driver.bus = &visorbus_type;
937 drv->driver.probe = visordriver_probe_device;
938 drv->driver.remove = visordriver_remove_device;
939 drv->driver.owner = drv->owner;
940
941 /*
942 * driver_register does this:
943 * bus_add_driver(drv)
944 * ->if (drv.bus) ** (bus_type) **
945 * driver_attach(drv)
946 * for each dev with bus type of drv.bus
947 * if (!dev.drv) ** no driver assigned yet **
948 * if (bus.match(dev,drv)) [visorbus_match]
949 * dev.drv = drv
950 * if (!drv.probe(dev)) [visordriver_probe_device]
951 * dev.drv = NULL
952 */
953
954 rc = driver_register(&drv->driver);
7a0ee694
DK
955 if (rc < 0)
956 driver_unregister(&drv->driver);
957 return rc;
958}
959EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
960
3fd1b3b6
DB
961/**
962 * create_bus_instance() - create a device instance for the visor bus itself
963 * @dev: struct visor_device indicating the bus instance
964 *
965 * Return: 0 for success, otherwise negative errno value indicating reason for
966 * failure
3703987c 967 */
d32517e3
DZ
968static int
969create_bus_instance(struct visor_device *dev)
3703987c 970{
d32517e3 971 int id = dev->chipset_bus_no;
8217becc 972 int err;
7726f813 973 struct spar_vbus_headerinfo *hdr_info;
3703987c 974
c6bc82f1 975 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC, 0, 0, DIAG_SEVERITY_PRINT);
7726f813
DZ
976
977 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
c2c667d6
BR
978 if (!hdr_info)
979 return -ENOMEM;
7726f813 980
343506bf
DZ
981 dev_set_name(&dev->device, "visorbus%d", id);
982 dev->device.bus = &visorbus_type;
983 dev->device.groups = visorbus_groups;
984 dev->device.release = visorbus_release_busdevice;
d32517e3 985
8217becc
TS
986 dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device),
987 visorbus_debugfs_dir);
988 if (!dev->debugfs_dir) {
989 err = -ENOMEM;
990 goto err_hdr_info;
991 }
992 dev->debugfs_client_bus_info =
b3d76e80 993 debugfs_create_file("client_bus_info", 0440,
8217becc
TS
994 dev->debugfs_dir, dev,
995 &client_bus_info_debugfs_fops);
996 if (!dev->debugfs_client_bus_info) {
997 err = -ENOMEM;
998 goto err_debugfs_dir;
999 }
1000
343506bf 1001 if (device_register(&dev->device) < 0) {
6daa8205 1002 POSTCODE_LINUX(DEVICE_CREATE_FAILURE_PC, 0, id,
c6bc82f1 1003 DIAG_SEVERITY_ERR);
8217becc
TS
1004 err = -ENODEV;
1005 goto err_debugfs_created;
3703987c 1006 }
d32517e3 1007
ee983d90
DZ
1008 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
1009 dev->vbus_hdr_info = (void *)hdr_info;
1010 write_vbus_chp_info(dev->visorchannel, hdr_info,
1011 &chipset_driverinfo);
1012 write_vbus_bus_info(dev->visorchannel, hdr_info,
1013 &clientbus_driverinfo);
b32c4997 1014 } else {
ee983d90 1015 kfree(hdr_info);
3703987c 1016 }
343506bf 1017 list_add_tail(&dev->list_all, &list_all_bus_instances);
343506bf 1018 dev_set_drvdata(&dev->device, dev);
d32517e3 1019 return 0;
8217becc
TS
1020
1021err_debugfs_created:
1022 debugfs_remove(dev->debugfs_client_bus_info);
1023
1024err_debugfs_dir:
1025 debugfs_remove_recursive(dev->debugfs_dir);
1026
1027err_hdr_info:
1028 kfree(hdr_info);
1029 return err;
3703987c
EA
1030}
1031
3fd1b3b6
DB
1032/**
1033 * remove_bus_instance() - remove a device instance for the visor bus itself
1034 * @dev: struct visor_device indentifying the bus to remove
3703987c
EA
1035 */
1036static void
343506bf 1037remove_bus_instance(struct visor_device *dev)
3703987c 1038{
3fd1b3b6
DB
1039 /*
1040 * Note that this will result in the release method for
343506bf 1041 * dev->dev being called, which will call
3703987c
EA
1042 * visorbus_release_busdevice(). This has something to do with
1043 * the put_device() done in device_unregister(), but I have never
1044 * successfully been able to trace thru the code to see where/how
1045 * release() gets called. But I know it does.
1046 */
343506bf
DZ
1047 if (dev->visorchannel) {
1048 visorchannel_destroy(dev->visorchannel);
1049 dev->visorchannel = NULL;
3703987c 1050 }
343506bf
DZ
1051 kfree(dev->vbus_hdr_info);
1052 list_del(&dev->list_all);
1053 device_unregister(&dev->device);
3703987c
EA
1054}
1055
3fd1b3b6
DB
1056/**
1057 * create_bus_type() - create and register the one-and-only one instance of
1058 * the visor bus type (visorbus_type)
1059 * Return: 0 for success, otherwise negative errno value returned by
1060 * bus_register() indicating the reason for failure
3703987c
EA
1061 */
1062static int
1063create_bus_type(void)
1064{
6155a3cf
BR
1065 busreg_rc = bus_register(&visorbus_type);
1066 return busreg_rc;
3703987c
EA
1067}
1068
3fd1b3b6
DB
1069/**
1070 * remove_bus_type() - remove the one-and-only one instance of the visor bus
1071 * type (visorbus_type)
3703987c
EA
1072 */
1073static void
1074remove_bus_type(void)
1075{
3703987c
EA
1076 bus_unregister(&visorbus_type);
1077}
1078
3fd1b3b6
DB
1079/**
1080 * remove_all_visor_devices() - remove all child visor bus device instances
3703987c
EA
1081 */
1082static void
1083remove_all_visor_devices(void)
1084{
1085 struct list_head *listentry, *listtmp;
1086
1087 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1088 struct visor_device *dev = list_entry(listentry,
1089 struct visor_device,
1090 list_all);
1091 remove_visor_device(dev);
1092 }
1093}
1094
87241ab8 1095void
d32517e3 1096chipset_bus_create(struct visor_device *dev)
3703987c 1097{
d32517e3
DZ
1098 int rc;
1099 u32 bus_no = dev->chipset_bus_no;
3703987c 1100
c6bc82f1 1101 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC, 0, bus_no, DIAG_SEVERITY_PRINT);
d32517e3 1102 rc = create_bus_instance(dev);
c6bc82f1 1103 POSTCODE_LINUX(BUS_CREATE_EXIT_PC, 0, bus_no, DIAG_SEVERITY_PRINT);
d32517e3
DZ
1104
1105 if (rc < 0)
6daa8205 1106 POSTCODE_LINUX(BUS_CREATE_FAILURE_PC, 0, bus_no,
c6bc82f1 1107 DIAG_SEVERITY_ERR);
d32517e3 1108 else
6daa8205 1109 POSTCODE_LINUX(CHIPSET_INIT_SUCCESS_PC, 0, bus_no,
c6bc82f1 1110 DIAG_SEVERITY_PRINT);
d32517e3 1111
87241ab8 1112 bus_create_response(dev, rc);
3703987c
EA
1113}
1114
87241ab8 1115void
d32517e3 1116chipset_bus_destroy(struct visor_device *dev)
3703987c 1117{
343506bf 1118 remove_bus_instance(dev);
87241ab8 1119 bus_destroy_response(dev, 0);
3703987c
EA
1120}
1121
87241ab8 1122void
a298bc0b 1123chipset_device_create(struct visor_device *dev_info)
3703987c 1124{
7a9749be 1125 int rc;
a298bc0b
DZ
1126 u32 bus_no = dev_info->chipset_bus_no;
1127 u32 dev_no = dev_info->chipset_dev_no;
3703987c 1128
f30c2c35 1129 POSTCODE_LINUX(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
c6bc82f1 1130 DIAG_SEVERITY_PRINT);
3703987c 1131
a298bc0b 1132 rc = create_visor_device(dev_info);
87241ab8 1133 device_create_response(dev_info, rc);
86ea8acc
DK
1134
1135 if (rc < 0)
f30c2c35 1136 POSTCODE_LINUX(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
c6bc82f1 1137 DIAG_SEVERITY_ERR);
86ea8acc 1138 else
f30c2c35 1139 POSTCODE_LINUX(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
c6bc82f1 1140 DIAG_SEVERITY_PRINT);
3703987c
EA
1141}
1142
87241ab8 1143void
a298bc0b 1144chipset_device_destroy(struct visor_device *dev_info)
3703987c 1145{
a298bc0b 1146 remove_visor_device(dev_info);
3703987c 1147
87241ab8 1148 device_destroy_response(dev_info, 0);
3703987c
EA
1149}
1150
3fd1b3b6
DB
1151/**
1152 * pause_state_change_complete() - the callback function to be called by a
1153 * visorbus function driver when a
1154 * pending "pause device" operation has
1155 * completed
1156 * @dev: struct visor_device identifying the paused device
1157 * @status: 0 iff the pause state change completed successfully, otherwise
1158 * a negative errno value indicating the reason for failure
3703987c
EA
1159 */
1160static void
a298bc0b 1161pause_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1162{
1163 if (!dev->pausing)
216c3e2c 1164 return;
3703987c 1165
779d0752 1166 dev->pausing = false;
3703987c 1167
ea3a5aaf 1168 device_pause_response(dev, status);
3703987c
EA
1169}
1170
3fd1b3b6
DB
1171/**
1172 * resume_state_change_complete() - the callback function to be called by a
1173 * visorbus function driver when a
1174 * pending "resume device" operation has
1175 * completed
1176 * @dev: struct visor_device identifying the resumed device
1177 * @status: 0 iff the resume state change completed successfully, otherwise
1178 * a negative errno value indicating the reason for failure
3703987c
EA
1179 */
1180static void
a298bc0b 1181resume_state_change_complete(struct visor_device *dev, int status)
3703987c
EA
1182{
1183 if (!dev->resuming)
216c3e2c 1184 return;
3703987c 1185
779d0752 1186 dev->resuming = false;
3703987c 1187
3fd1b3b6
DB
1188 /*
1189 * Notify the chipset driver that the resume is complete,
3703987c 1190 * which will presumably want to send some sort of response to
48117895
GL
1191 * the initiator.
1192 */
87241ab8 1193 device_resume_response(dev, status);
3703987c
EA
1194}
1195
3fd1b3b6
DB
1196/**
1197 * initiate_chipset_device_pause_resume() - start a pause or resume operation
1198 * for a visor device
1199 * @dev: struct visor_device identifying the device being paused or resumed
1200 * @is_pause: true to indicate pause operation, false to indicate resume
1201 *
1202 * Tell the subordinate function driver for a specific device to pause
1203 * or resume that device. Success/failure result is returned asynchronously
1204 * via a callback function; see pause_state_change_complete() and
1205 * resume_state_change_complete().
3703987c
EA
1206 */
1207static void
a298bc0b 1208initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
3703987c 1209{
03b93f08 1210 int rc;
3703987c 1211 struct visor_driver *drv = NULL;
a298bc0b 1212 void (*notify_func)(struct visor_device *dev, int response) = NULL;
3703987c
EA
1213
1214 if (is_pause)
ea3a5aaf 1215 notify_func = device_pause_response;
3703987c 1216 else
87241ab8 1217 notify_func = device_resume_response;
3703987c 1218 if (!notify_func)
03b93f08 1219 return;
3703987c 1220
3703987c 1221 drv = to_visor_driver(dev->device.driver);
03b93f08
BR
1222 if (!drv) {
1223 (*notify_func)(dev, -ENODEV);
1224 return;
1225 }
3703987c 1226
03b93f08
BR
1227 if (dev->pausing || dev->resuming) {
1228 (*notify_func)(dev, -EBUSY);
1229 return;
1230 }
3703987c 1231
3fd1b3b6
DB
1232 /*
1233 * Note that even though both drv->pause() and drv->resume
3703987c
EA
1234 * specify a callback function, it is NOT necessary for us to
1235 * increment our local module usage count. Reason is, there
1236 * is already a linkage dependency between child function
1237 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1238 * visorbus while child function drivers are still running.
1239 */
1240 if (is_pause) {
03b93f08
BR
1241 if (!drv->pause) {
1242 (*notify_func)(dev, -EINVAL);
1243 return;
1244 }
3703987c 1245
779d0752 1246 dev->pausing = true;
03b93f08 1247 rc = drv->pause(dev, pause_state_change_complete);
3703987c
EA
1248 } else {
1249 /* This should be done at BUS resume time, but an
1250 * existing problem prevents us from ever getting a bus
1251 * resume... This hack would fail to work should we
1252 * ever have a bus that contains NO devices, since we
48117895
GL
1253 * would never even get here in that case.
1254 */
3703987c 1255 fix_vbus_dev_info(dev);
03b93f08
BR
1256 if (!drv->resume) {
1257 (*notify_func)(dev, -EINVAL);
1258 return;
1259 }
3703987c 1260
779d0752 1261 dev->resuming = true;
03b93f08 1262 rc = drv->resume(dev, resume_state_change_complete);
3703987c 1263 }
03b93f08 1264 if (rc < 0) {
3703987c 1265 if (is_pause)
779d0752 1266 dev->pausing = false;
3703987c 1267 else
779d0752 1268 dev->resuming = false;
03b93f08 1269 (*notify_func)(dev, -EINVAL);
3703987c
EA
1270 }
1271}
1272
3fd1b3b6
DB
1273/**
1274 * chipset_device_pause() - start a pause operation for a visor device
1275 * @dev_info: struct visor_device identifying the device being paused
1276 *
1277 * Tell the subordinate function driver for a specific device to pause
1278 * that device. Success/failure result is returned asynchronously
1279 * via a callback function; see pause_state_change_complete().
1280 */
87241ab8 1281void
a298bc0b 1282chipset_device_pause(struct visor_device *dev_info)
3703987c 1283{
b4b598fd 1284 initiate_chipset_device_pause_resume(dev_info, true);
3703987c
EA
1285}
1286
3fd1b3b6
DB
1287/**
1288 * chipset_device_resume() - start a resume operation for a visor device
1289 * @dev_info: struct visor_device identifying the device being resumed
1290 *
1291 * Tell the subordinate function driver for a specific device to resume
1292 * that device. Success/failure result is returned asynchronously
1293 * via a callback function; see resume_state_change_complete().
1294 */
87241ab8 1295void
a298bc0b 1296chipset_device_resume(struct visor_device *dev_info)
3703987c 1297{
b4b598fd 1298 initiate_chipset_device_pause_resume(dev_info, false);
3703987c
EA
1299}
1300
55c67dca 1301int
3703987c
EA
1302visorbus_init(void)
1303{
78af1aef 1304 int err;
3703987c 1305
c6bc82f1 1306 POSTCODE_LINUX(DRIVER_ENTRY_PC, 0, 0, DIAG_SEVERITY_PRINT);
8217becc
TS
1307
1308 visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL);
1309 if (!visorbus_debugfs_dir)
1310 return -ENOMEM;
1311
e82ed633 1312 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
3703987c 1313
78af1aef
DK
1314 err = create_bus_type();
1315 if (err < 0) {
3840b772 1316 POSTCODE_LINUX(BUS_CREATE_ENTRY_PC, 0, 0, DIAG_SEVERITY_ERR);
78af1aef 1317 goto error;
3703987c
EA
1318 }
1319
e82ed633 1320 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
3703987c 1321
78af1aef 1322 return 0;
3703987c 1323
78af1aef 1324error:
c6bc82f1 1325 POSTCODE_LINUX(CHIPSET_INIT_FAILURE_PC, 0, err, DIAG_SEVERITY_ERR);
78af1aef 1326 return err;
3703987c
EA
1327}
1328
c79b28f7 1329void
3703987c
EA
1330visorbus_exit(void)
1331{
1332 struct list_head *listentry, *listtmp;
1333
3703987c
EA
1334 remove_all_visor_devices();
1335
3703987c 1336 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
343506bf 1337 struct visor_device *dev = list_entry(listentry,
216c3e2c
CJ
1338 struct visor_device,
1339 list_all);
343506bf 1340 remove_bus_instance(dev);
3703987c
EA
1341 }
1342 remove_bus_type();
8217becc 1343 debugfs_remove_recursive(visorbus_debugfs_dir);
3703987c
EA
1344}
1345
b3d76e80 1346module_param_named(forcematch, visorbus_forcematch, int, 0444);
3703987c
EA
1347MODULE_PARM_DESC(visorbus_forcematch,
1348 "1 to force a successful dev <--> drv match");
3703987c 1349
b3d76e80 1350module_param_named(forcenomatch, visorbus_forcenomatch, int, 0444);
3703987c
EA
1351MODULE_PARM_DESC(visorbus_forcenomatch,
1352 "1 to force an UNsuccessful dev <--> drv match");