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