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