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