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