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