xref: /linux/drivers/platform/x86/uv_sysfs.c (revision bf4afc53b77aeaa48b5409da5c8da6bb4eff7f43)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * This file supports the /sys/firmware/sgi_uv topology tree on HPE UV.
4  *
5  *  Copyright (c) 2020 Hewlett Packard Enterprise.  All Rights Reserved.
6  *  Copyright (c) Justin Ernst
7  */
8 
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/kobject.h>
14 #include <linux/vmalloc.h>
15 #include <asm/uv/bios.h>
16 #include <asm/uv/uv.h>
17 #include <asm/uv/uv_hub.h>
18 #include <asm/uv/uv_geo.h>
19 
20 #define INVALID_CNODE -1
21 
22 struct kobject *sgi_uv_kobj;
23 static struct kset *uv_pcibus_kset;
24 static struct kset *uv_hubs_kset;
25 static struct uv_bios_hub_info *hub_buf;
26 static struct uv_bios_port_info **port_buf;
27 static struct uv_hub **uv_hubs;
28 static struct uv_pci_top_obj **uv_pci_objs;
29 static int num_pci_lines;
30 static int num_cnodes;
31 static int *prev_obj_to_cnode;
32 static int uv_bios_obj_cnt;
33 static signed short uv_master_nasid = -1;
34 static void *uv_biosheap;
35 
36 static const char *uv_type_string(void)
37 {
38 	if (is_uv5_hub())
39 		return "9.0";
40 	else if (is_uv4a_hub())
41 		return "7.1";
42 	else if (is_uv4_hub())
43 		return "7.0";
44 	else if (is_uv3_hub())
45 		return "5.0";
46 	else if (is_uv2_hub())
47 		return "3.0";
48 	else if (uv_get_hubless_system())
49 		return "0.1";
50 	else
51 		return "unknown";
52 }
53 
54 static int ordinal_to_nasid(int ordinal)
55 {
56 	if (ordinal < num_cnodes && ordinal >= 0)
57 		return UV_PNODE_TO_NASID(uv_blade_to_pnode(ordinal));
58 	else
59 		return -1;
60 }
61 
62 static union geoid_u cnode_to_geoid(int cnode)
63 {
64 	union geoid_u geoid;
65 
66 	uv_bios_get_geoinfo(ordinal_to_nasid(cnode), (u64)sizeof(union geoid_u), (u64 *)&geoid);
67 	return geoid;
68 }
69 
70 static int location_to_bpos(char *location, int *rack, int *slot, int *blade)
71 {
72 	char type, r, b, h;
73 	int idb, idh;
74 
75 	if (sscanf(location, "%c%03d%c%02d%c%2d%c%d",
76 			 &r, rack, &type, slot, &b, &idb, &h, &idh) != 8)
77 		return -1;
78 	*blade = idb * 2 + idh;
79 
80 	return 0;
81 }
82 
83 static int cache_obj_to_cnode(struct uv_bios_hub_info *obj)
84 {
85 	int cnode;
86 	union geoid_u geoid;
87 	int obj_rack, obj_slot, obj_blade;
88 	int rack, slot, blade;
89 
90 	if (!obj->f.fields.this_part && !obj->f.fields.is_shared)
91 		return 0;
92 
93 	if (location_to_bpos(obj->location, &obj_rack, &obj_slot, &obj_blade))
94 		return -1;
95 
96 	for (cnode = 0; cnode < num_cnodes; cnode++) {
97 		geoid = cnode_to_geoid(cnode);
98 		rack = geo_rack(geoid);
99 		slot = geo_slot(geoid);
100 		blade = geo_blade(geoid);
101 		if (obj_rack == rack && obj_slot == slot && obj_blade == blade)
102 			prev_obj_to_cnode[obj->id] = cnode;
103 	}
104 
105 	return 0;
106 }
107 
108 static int get_obj_to_cnode(int obj_id)
109 {
110 	return prev_obj_to_cnode[obj_id];
111 }
112 
113 struct uv_hub {
114 	struct kobject kobj;
115 	struct uv_bios_hub_info *hub_info;
116 	struct uv_port **ports;
117 };
118 
119 #define to_uv_hub(kobj_ptr) container_of(kobj_ptr, struct uv_hub, kobj)
120 
121 static ssize_t hub_name_show(struct uv_bios_hub_info *hub_info, char *buf)
122 {
123 	return sysfs_emit(buf, "%s\n", hub_info->name);
124 }
125 
126 static ssize_t hub_location_show(struct uv_bios_hub_info *hub_info, char *buf)
127 {
128 	return sysfs_emit(buf, "%s\n", hub_info->location);
129 }
130 
131 static ssize_t hub_partition_show(struct uv_bios_hub_info *hub_info, char *buf)
132 {
133 	return sysfs_emit(buf, "%d\n", hub_info->f.fields.this_part);
134 }
135 
136 static ssize_t hub_shared_show(struct uv_bios_hub_info *hub_info, char *buf)
137 {
138 	return sysfs_emit(buf, "%d\n", hub_info->f.fields.is_shared);
139 }
140 static ssize_t hub_nasid_show(struct uv_bios_hub_info *hub_info, char *buf)
141 {
142 	int cnode = get_obj_to_cnode(hub_info->id);
143 
144 	return sysfs_emit(buf, "%d\n", ordinal_to_nasid(cnode));
145 }
146 static ssize_t hub_cnode_show(struct uv_bios_hub_info *hub_info, char *buf)
147 {
148 	return sysfs_emit(buf, "%d\n", get_obj_to_cnode(hub_info->id));
149 }
150 
151 struct hub_sysfs_entry {
152 	struct attribute attr;
153 	ssize_t (*show)(struct uv_bios_hub_info *hub_info, char *buf);
154 	ssize_t (*store)(struct uv_bios_hub_info *hub_info, const char *buf, size_t sz);
155 };
156 
157 static struct hub_sysfs_entry name_attribute =
158 	__ATTR(name, 0444, hub_name_show, NULL);
159 static struct hub_sysfs_entry location_attribute =
160 	__ATTR(location, 0444, hub_location_show, NULL);
161 static struct hub_sysfs_entry partition_attribute =
162 	__ATTR(this_partition, 0444, hub_partition_show, NULL);
163 static struct hub_sysfs_entry shared_attribute =
164 	__ATTR(shared, 0444, hub_shared_show, NULL);
165 static struct hub_sysfs_entry nasid_attribute =
166 	__ATTR(nasid, 0444, hub_nasid_show, NULL);
167 static struct hub_sysfs_entry cnode_attribute =
168 	__ATTR(cnode, 0444, hub_cnode_show, NULL);
169 
170 static struct attribute *uv_hub_attrs[] = {
171 	&name_attribute.attr,
172 	&location_attribute.attr,
173 	&partition_attribute.attr,
174 	&shared_attribute.attr,
175 	&nasid_attribute.attr,
176 	&cnode_attribute.attr,
177 	NULL,
178 };
179 ATTRIBUTE_GROUPS(uv_hub);
180 
181 static void hub_release(struct kobject *kobj)
182 {
183 	struct uv_hub *hub = to_uv_hub(kobj);
184 
185 	kfree(hub);
186 }
187 
188 static ssize_t hub_type_show(struct kobject *kobj, struct attribute *attr,
189 				char *buf)
190 {
191 	struct uv_hub *hub = to_uv_hub(kobj);
192 	struct uv_bios_hub_info *bios_hub_info = hub->hub_info;
193 	struct hub_sysfs_entry *entry;
194 
195 	entry = container_of(attr, struct hub_sysfs_entry, attr);
196 
197 	if (!entry->show)
198 		return -EIO;
199 
200 	return entry->show(bios_hub_info, buf);
201 }
202 
203 static const struct sysfs_ops hub_sysfs_ops = {
204 	.show = hub_type_show,
205 };
206 
207 static const struct kobj_type hub_attr_type = {
208 	.release	= hub_release,
209 	.sysfs_ops	= &hub_sysfs_ops,
210 	.default_groups	= uv_hub_groups,
211 };
212 
213 static int uv_hubs_init(void)
214 {
215 	s64 biosr;
216 	u64 sz;
217 	int i, ret;
218 
219 	prev_obj_to_cnode = kmalloc_objs(*prev_obj_to_cnode, uv_bios_obj_cnt,
220 					 GFP_KERNEL);
221 	if (!prev_obj_to_cnode)
222 		return -ENOMEM;
223 
224 	for (i = 0; i < uv_bios_obj_cnt; i++)
225 		prev_obj_to_cnode[i] = INVALID_CNODE;
226 
227 	uv_hubs_kset = kset_create_and_add("hubs", NULL, sgi_uv_kobj);
228 	if (!uv_hubs_kset) {
229 		ret = -ENOMEM;
230 		goto err_hubs_kset;
231 	}
232 	sz = uv_bios_obj_cnt * sizeof(*hub_buf);
233 	hub_buf = kzalloc(sz, GFP_KERNEL);
234 	if (!hub_buf) {
235 		ret = -ENOMEM;
236 		goto err_hub_buf;
237 	}
238 
239 	biosr = uv_bios_enum_objs((u64)uv_master_nasid, sz, (u64 *)hub_buf);
240 	if (biosr) {
241 		ret = -EINVAL;
242 		goto err_enum_objs;
243 	}
244 
245 	uv_hubs = kzalloc_objs(*uv_hubs, uv_bios_obj_cnt);
246 	if (!uv_hubs) {
247 		ret = -ENOMEM;
248 		goto err_enum_objs;
249 	}
250 
251 	for (i = 0; i < uv_bios_obj_cnt; i++) {
252 		uv_hubs[i] = kzalloc_obj(*uv_hubs[i]);
253 		if (!uv_hubs[i]) {
254 			i--;
255 			ret = -ENOMEM;
256 			goto err_hubs;
257 		}
258 
259 		uv_hubs[i]->hub_info = &hub_buf[i];
260 		cache_obj_to_cnode(uv_hubs[i]->hub_info);
261 
262 		uv_hubs[i]->kobj.kset = uv_hubs_kset;
263 
264 		ret = kobject_init_and_add(&uv_hubs[i]->kobj, &hub_attr_type,
265 					  NULL, "hub_%u", hub_buf[i].id);
266 		if (ret)
267 			goto err_hubs;
268 		kobject_uevent(&uv_hubs[i]->kobj, KOBJ_ADD);
269 	}
270 	return 0;
271 
272 err_hubs:
273 	for (; i >= 0; i--)
274 		kobject_put(&uv_hubs[i]->kobj);
275 	kfree(uv_hubs);
276 err_enum_objs:
277 	kfree(hub_buf);
278 err_hub_buf:
279 	kset_unregister(uv_hubs_kset);
280 err_hubs_kset:
281 	kfree(prev_obj_to_cnode);
282 	return ret;
283 
284 }
285 
286 static void uv_hubs_exit(void)
287 {
288 	int i;
289 
290 	for (i = 0; i < uv_bios_obj_cnt; i++)
291 		kobject_put(&uv_hubs[i]->kobj);
292 
293 	kfree(uv_hubs);
294 	kfree(hub_buf);
295 	kset_unregister(uv_hubs_kset);
296 	kfree(prev_obj_to_cnode);
297 }
298 
299 struct uv_port {
300 	struct kobject kobj;
301 	struct uv_bios_port_info *port_info;
302 };
303 
304 #define to_uv_port(kobj_ptr) container_of(kobj_ptr, struct uv_port, kobj)
305 
306 static ssize_t uv_port_conn_hub_show(struct uv_bios_port_info *port, char *buf)
307 {
308 	return sysfs_emit(buf, "%d\n", port->conn_id);
309 }
310 
311 static ssize_t uv_port_conn_port_show(struct uv_bios_port_info *port, char *buf)
312 {
313 	return sysfs_emit(buf, "%d\n", port->conn_port);
314 }
315 
316 struct uv_port_sysfs_entry {
317 	struct attribute attr;
318 	ssize_t (*show)(struct uv_bios_port_info *port_info, char *buf);
319 	ssize_t (*store)(struct uv_bios_port_info *port_info, const char *buf, size_t size);
320 };
321 
322 static struct uv_port_sysfs_entry uv_port_conn_hub_attribute =
323 	__ATTR(conn_hub, 0444, uv_port_conn_hub_show, NULL);
324 static struct uv_port_sysfs_entry uv_port_conn_port_attribute =
325 	__ATTR(conn_port, 0444, uv_port_conn_port_show, NULL);
326 
327 static struct attribute *uv_port_attrs[] = {
328 	&uv_port_conn_hub_attribute.attr,
329 	&uv_port_conn_port_attribute.attr,
330 	NULL,
331 };
332 ATTRIBUTE_GROUPS(uv_port);
333 
334 static void uv_port_release(struct kobject *kobj)
335 {
336 	struct uv_port *port = to_uv_port(kobj);
337 
338 	kfree(port);
339 }
340 
341 static ssize_t uv_port_type_show(struct kobject *kobj, struct attribute *attr,
342 				char *buf)
343 {
344 	struct uv_port *port = to_uv_port(kobj);
345 	struct uv_bios_port_info *port_info = port->port_info;
346 	struct uv_port_sysfs_entry *entry;
347 
348 	entry = container_of(attr, struct uv_port_sysfs_entry, attr);
349 
350 	if (!entry->show)
351 		return -EIO;
352 
353 	return entry->show(port_info, buf);
354 }
355 
356 static const struct sysfs_ops uv_port_sysfs_ops = {
357 	.show = uv_port_type_show,
358 };
359 
360 static const struct kobj_type uv_port_attr_type = {
361 	.release	= uv_port_release,
362 	.sysfs_ops	= &uv_port_sysfs_ops,
363 	.default_groups	= uv_port_groups,
364 };
365 
366 static int uv_ports_init(void)
367 {
368 	s64 biosr;
369 	int j = 0, k = 0, ret, sz;
370 
371 	port_buf = kzalloc_objs(*port_buf, uv_bios_obj_cnt);
372 	if (!port_buf)
373 		return -ENOMEM;
374 
375 	for (j = 0; j < uv_bios_obj_cnt; j++) {
376 		sz = hub_buf[j].ports * sizeof(*port_buf[j]);
377 		port_buf[j] = kzalloc(sz, GFP_KERNEL);
378 		if (!port_buf[j]) {
379 			ret = -ENOMEM;
380 			j--;
381 			goto err_port_info;
382 		}
383 		biosr = uv_bios_enum_ports((u64)uv_master_nasid, (u64)hub_buf[j].id, sz,
384 					(u64 *)port_buf[j]);
385 		if (biosr) {
386 			ret = -EINVAL;
387 			goto err_port_info;
388 		}
389 	}
390 	for (j = 0; j < uv_bios_obj_cnt; j++) {
391 		uv_hubs[j]->ports = kzalloc_objs(*uv_hubs[j]->ports,
392 						 hub_buf[j].ports, GFP_KERNEL);
393 		if (!uv_hubs[j]->ports) {
394 			ret = -ENOMEM;
395 			j--;
396 			goto err_ports;
397 		}
398 	}
399 	for (j = 0; j < uv_bios_obj_cnt; j++) {
400 		for (k = 0; k < hub_buf[j].ports; k++) {
401 			uv_hubs[j]->ports[k] = kzalloc_obj(*uv_hubs[j]->ports[k],
402 							   GFP_KERNEL);
403 			if (!uv_hubs[j]->ports[k]) {
404 				ret = -ENOMEM;
405 				k--;
406 				goto err_kobj_ports;
407 			}
408 			uv_hubs[j]->ports[k]->port_info = &port_buf[j][k];
409 			ret = kobject_init_and_add(&uv_hubs[j]->ports[k]->kobj, &uv_port_attr_type,
410 					&uv_hubs[j]->kobj, "port_%d", port_buf[j][k].port);
411 			if (ret)
412 				goto err_kobj_ports;
413 			kobject_uevent(&uv_hubs[j]->ports[k]->kobj, KOBJ_ADD);
414 		}
415 	}
416 	return 0;
417 
418 err_kobj_ports:
419 	for (; j >= 0; j--) {
420 		for (; k >= 0; k--)
421 			kobject_put(&uv_hubs[j]->ports[k]->kobj);
422 		if (j > 0)
423 			k = hub_buf[j-1].ports - 1;
424 	}
425 	j = uv_bios_obj_cnt - 1;
426 err_ports:
427 	for (; j >= 0; j--)
428 		kfree(uv_hubs[j]->ports);
429 	j = uv_bios_obj_cnt - 1;
430 err_port_info:
431 	for (; j >= 0; j--)
432 		kfree(port_buf[j]);
433 	kfree(port_buf);
434 	return ret;
435 }
436 
437 static void uv_ports_exit(void)
438 {
439 	int j, k;
440 
441 	for (j = 0; j < uv_bios_obj_cnt; j++) {
442 		for (k = hub_buf[j].ports - 1; k >= 0; k--)
443 			kobject_put(&uv_hubs[j]->ports[k]->kobj);
444 	}
445 	for (j = 0; j < uv_bios_obj_cnt; j++) {
446 		kfree(uv_hubs[j]->ports);
447 		kfree(port_buf[j]);
448 	}
449 	kfree(port_buf);
450 }
451 
452 struct uv_pci_top_obj {
453 	struct kobject kobj;
454 	char *type;
455 	char *location;
456 	int iio_stack;
457 	char *ppb_addr;
458 	int slot;
459 };
460 
461 #define to_uv_pci_top_obj(kobj_ptr) container_of(kobj_ptr, struct uv_pci_top_obj, kobj)
462 
463 static ssize_t uv_pci_type_show(struct uv_pci_top_obj *top_obj, char *buf)
464 {
465 	return sysfs_emit(buf, "%s\n", top_obj->type);
466 }
467 
468 static ssize_t uv_pci_location_show(struct uv_pci_top_obj *top_obj, char *buf)
469 {
470 	return sysfs_emit(buf, "%s\n", top_obj->location);
471 }
472 
473 static ssize_t uv_pci_iio_stack_show(struct uv_pci_top_obj *top_obj, char *buf)
474 {
475 	return sysfs_emit(buf, "%d\n", top_obj->iio_stack);
476 }
477 
478 static ssize_t uv_pci_ppb_addr_show(struct uv_pci_top_obj *top_obj, char *buf)
479 {
480 	return sysfs_emit(buf, "%s\n", top_obj->ppb_addr);
481 }
482 
483 static ssize_t uv_pci_slot_show(struct uv_pci_top_obj *top_obj, char *buf)
484 {
485 	return sysfs_emit(buf, "%d\n", top_obj->slot);
486 }
487 
488 struct uv_pci_top_sysfs_entry {
489 	struct attribute attr;
490 	ssize_t (*show)(struct uv_pci_top_obj *top_obj, char *buf);
491 	ssize_t (*store)(struct uv_pci_top_obj *top_obj, const char *buf, size_t size);
492 };
493 
494 static struct uv_pci_top_sysfs_entry uv_pci_type_attribute =
495 	__ATTR(type, 0444, uv_pci_type_show, NULL);
496 static struct uv_pci_top_sysfs_entry uv_pci_location_attribute =
497 	__ATTR(location, 0444, uv_pci_location_show, NULL);
498 static struct uv_pci_top_sysfs_entry uv_pci_iio_stack_attribute =
499 	__ATTR(iio_stack, 0444, uv_pci_iio_stack_show, NULL);
500 static struct uv_pci_top_sysfs_entry uv_pci_ppb_addr_attribute =
501 	__ATTR(ppb_addr, 0444, uv_pci_ppb_addr_show, NULL);
502 static struct uv_pci_top_sysfs_entry uv_pci_slot_attribute =
503 	__ATTR(slot, 0444, uv_pci_slot_show, NULL);
504 
505 static void uv_pci_top_release(struct kobject *kobj)
506 {
507 	struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
508 
509 	kfree(top_obj->type);
510 	kfree(top_obj->location);
511 	kfree(top_obj->ppb_addr);
512 	kfree(top_obj);
513 }
514 
515 static ssize_t pci_top_type_show(struct kobject *kobj,
516 			struct attribute *attr, char *buf)
517 {
518 	struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
519 	struct uv_pci_top_sysfs_entry *entry;
520 
521 	entry = container_of(attr, struct uv_pci_top_sysfs_entry, attr);
522 
523 	if (!entry->show)
524 		return -EIO;
525 
526 	return entry->show(top_obj, buf);
527 }
528 
529 static const struct sysfs_ops uv_pci_top_sysfs_ops = {
530 	.show = pci_top_type_show,
531 };
532 
533 static const struct kobj_type uv_pci_top_attr_type = {
534 	.release	= uv_pci_top_release,
535 	.sysfs_ops	= &uv_pci_top_sysfs_ops,
536 };
537 
538 static int init_pci_top_obj(struct uv_pci_top_obj *top_obj, char *line)
539 {
540 	char *start;
541 	char type[11], location[14], ppb_addr[15];
542 	int str_cnt, ret;
543 	unsigned int tmp_match[2];
544 
545 	// Minimum line length
546 	if (strlen(line) < 36)
547 		return -EINVAL;
548 
549 	//Line must match format "pcibus %4x:%2x" to be valid
550 	str_cnt = sscanf(line, "pcibus %4x:%2x", &tmp_match[0], &tmp_match[1]);
551 	if (str_cnt < 2)
552 		return -EINVAL;
553 
554 	/* Connect pcibus to segment:bus number with '_'
555 	 * to concatenate name tokens.
556 	 * pcibus 0000:00 ... -> pcibus_0000:00 ...
557 	 */
558 	line[6] = '_';
559 
560 	/* Null terminate after the concatencated name tokens
561 	 * to produce kobj name string.
562 	 */
563 	line[14] = '\0';
564 
565 	// Use start to index after name tokens string for remainder of line info.
566 	start = &line[15];
567 
568 	top_obj->iio_stack = -1;
569 	top_obj->slot = -1;
570 
571 	/* r001i01b00h0 BASE IO (IIO Stack 0)
572 	 * r001i01b00h1 PCIe IO (IIO Stack 1)
573 	 * r001i01b03h1 PCIe SLOT
574 	 * r001i01b00h0 NODE IO
575 	 * r001i01b00h0 Riser
576 	 * (IIO Stack #) may not be present.
577 	 */
578 	if (start[0] == 'r') {
579 		str_cnt = sscanf(start, "%13s %10[^(] %*s %*s %d)",
580 				location, type, &top_obj->iio_stack);
581 		if (str_cnt < 2)
582 			return -EINVAL;
583 		top_obj->type = kstrdup(type, GFP_KERNEL);
584 		if (!top_obj->type)
585 			return -ENOMEM;
586 		top_obj->location = kstrdup(location, GFP_KERNEL);
587 		if (!top_obj->location) {
588 			kfree(top_obj->type);
589 			return -ENOMEM;
590 		}
591 	}
592 	/* PPB at 0000:80:00.00 (slot 3)
593 	 * (slot #) may not be present.
594 	 */
595 	else if (start[0] == 'P') {
596 		str_cnt = sscanf(start, "%10s %*s %14s %*s %d)",
597 				type, ppb_addr, &top_obj->slot);
598 		if (str_cnt < 2)
599 			return -EINVAL;
600 		top_obj->type = kstrdup(type, GFP_KERNEL);
601 		if (!top_obj->type)
602 			return -ENOMEM;
603 		top_obj->ppb_addr = kstrdup(ppb_addr, GFP_KERNEL);
604 		if (!top_obj->ppb_addr) {
605 			kfree(top_obj->type);
606 			return -ENOMEM;
607 		}
608 	} else
609 		return -EINVAL;
610 
611 	top_obj->kobj.kset = uv_pcibus_kset;
612 
613 	ret = kobject_init_and_add(&top_obj->kobj, &uv_pci_top_attr_type, NULL, "%s", line);
614 	if (ret)
615 		goto err_add_sysfs;
616 
617 	if (top_obj->type) {
618 		ret = sysfs_create_file(&top_obj->kobj, &uv_pci_type_attribute.attr);
619 		if (ret)
620 			goto err_add_sysfs;
621 	}
622 	if (top_obj->location) {
623 		ret = sysfs_create_file(&top_obj->kobj, &uv_pci_location_attribute.attr);
624 		if (ret)
625 			goto err_add_sysfs;
626 	}
627 	if (top_obj->iio_stack >= 0) {
628 		ret = sysfs_create_file(&top_obj->kobj, &uv_pci_iio_stack_attribute.attr);
629 		if (ret)
630 			goto err_add_sysfs;
631 	}
632 	if (top_obj->ppb_addr) {
633 		ret = sysfs_create_file(&top_obj->kobj, &uv_pci_ppb_addr_attribute.attr);
634 		if (ret)
635 			goto err_add_sysfs;
636 	}
637 	if (top_obj->slot >= 0) {
638 		ret = sysfs_create_file(&top_obj->kobj, &uv_pci_slot_attribute.attr);
639 		if (ret)
640 			goto err_add_sysfs;
641 	}
642 
643 	kobject_uevent(&top_obj->kobj, KOBJ_ADD);
644 	return 0;
645 
646 err_add_sysfs:
647 	kobject_put(&top_obj->kobj);
648 	return ret;
649 }
650 
651 static int pci_topology_init(void)
652 {
653 	char *pci_top_str, *start, *found, *count;
654 	size_t sz;
655 	s64 biosr;
656 	int l = 0, k = 0;
657 	int len, ret;
658 
659 	uv_pcibus_kset = kset_create_and_add("pcibuses", NULL, sgi_uv_kobj);
660 	if (!uv_pcibus_kset)
661 		return -ENOMEM;
662 
663 	for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
664 		pci_top_str = kmalloc(sz, GFP_KERNEL);
665 		if (!pci_top_str) {
666 			ret = -ENOMEM;
667 			goto err_pci_top_str;
668 		}
669 		biosr = uv_bios_get_pci_topology((u64)sz, (u64 *)pci_top_str);
670 		if (biosr == BIOS_STATUS_SUCCESS) {
671 			len = strnlen(pci_top_str, sz);
672 			for (count = pci_top_str; count < pci_top_str + len; count++) {
673 				if (*count == '\n')
674 					l++;
675 			}
676 			num_pci_lines = l;
677 
678 			uv_pci_objs = kzalloc_objs(*uv_pci_objs, num_pci_lines,
679 						   GFP_KERNEL);
680 			if (!uv_pci_objs) {
681 				kfree(pci_top_str);
682 				ret = -ENOMEM;
683 				goto err_pci_top_str;
684 			}
685 			start = pci_top_str;
686 			while ((found = strsep(&start, "\n")) != NULL) {
687 				uv_pci_objs[k] = kzalloc_obj(*uv_pci_objs[k],
688 							     GFP_KERNEL);
689 				if (!uv_pci_objs[k]) {
690 					ret = -ENOMEM;
691 					goto err_pci_obj;
692 				}
693 				ret = init_pci_top_obj(uv_pci_objs[k], found);
694 				if (ret)
695 					goto err_pci_obj;
696 				k++;
697 				if (k == num_pci_lines)
698 					break;
699 			}
700 		}
701 		kfree(pci_top_str);
702 		if (biosr == BIOS_STATUS_SUCCESS || biosr == BIOS_STATUS_UNIMPLEMENTED)
703 			break;
704 	}
705 
706 	return 0;
707 err_pci_obj:
708 	k--;
709 	for (; k >= 0; k--)
710 		kobject_put(&uv_pci_objs[k]->kobj);
711 	kfree(uv_pci_objs);
712 	kfree(pci_top_str);
713 err_pci_top_str:
714 	kset_unregister(uv_pcibus_kset);
715 	return ret;
716 }
717 
718 static void pci_topology_exit(void)
719 {
720 	int k;
721 
722 	for (k = 0; k < num_pci_lines; k++)
723 		kobject_put(&uv_pci_objs[k]->kobj);
724 	kset_unregister(uv_pcibus_kset);
725 	kfree(uv_pci_objs);
726 }
727 
728 static ssize_t partition_id_show(struct kobject *kobj,
729 			struct kobj_attribute *attr, char *buf)
730 {
731 	return sysfs_emit(buf, "%ld\n", sn_partition_id);
732 }
733 
734 static ssize_t coherence_id_show(struct kobject *kobj,
735 			struct kobj_attribute *attr, char *buf)
736 {
737 	return sysfs_emit(buf, "%ld\n", sn_coherency_id);
738 }
739 
740 static ssize_t uv_type_show(struct kobject *kobj,
741 			struct kobj_attribute *attr, char *buf)
742 {
743 	return sysfs_emit(buf, "%s\n", uv_type_string());
744 }
745 
746 static ssize_t uv_archtype_show(struct kobject *kobj,
747 			struct kobj_attribute *attr, char *buf)
748 {
749 	return uv_get_archtype(buf, PAGE_SIZE);
750 }
751 
752 static ssize_t uv_hub_type_show(struct kobject *kobj,
753 			struct kobj_attribute *attr, char *buf)
754 {
755 	return sysfs_emit(buf, "0x%x\n", uv_hub_type());
756 }
757 
758 static ssize_t uv_hubless_show(struct kobject *kobj,
759 			struct kobj_attribute *attr, char *buf)
760 {
761 	return sysfs_emit(buf, "0x%x\n", uv_get_hubless_system());
762 }
763 
764 static struct kobj_attribute partition_id_attr =
765 	__ATTR(partition_id, 0444, partition_id_show, NULL);
766 static struct kobj_attribute coherence_id_attr =
767 	__ATTR(coherence_id, 0444, coherence_id_show, NULL);
768 static struct kobj_attribute uv_type_attr =
769 	__ATTR(uv_type, 0444, uv_type_show, NULL);
770 static struct kobj_attribute uv_archtype_attr =
771 	__ATTR(archtype, 0444, uv_archtype_show, NULL);
772 static struct kobj_attribute uv_hub_type_attr =
773 	__ATTR(hub_type, 0444, uv_hub_type_show, NULL);
774 static struct kobj_attribute uv_hubless_attr =
775 	__ATTR(hubless, 0444, uv_hubless_show, NULL);
776 
777 static struct attribute *base_attrs[] = {
778 	&partition_id_attr.attr,
779 	&coherence_id_attr.attr,
780 	&uv_type_attr.attr,
781 	&uv_archtype_attr.attr,
782 	&uv_hub_type_attr.attr,
783 	NULL,
784 };
785 
786 static const struct attribute_group base_attr_group = {
787 	.attrs = base_attrs
788 };
789 
790 static int initial_bios_setup(void)
791 {
792 	u64 v;
793 	s64 biosr;
794 
795 	biosr = uv_bios_get_master_nasid((u64)sizeof(uv_master_nasid), (u64 *)&uv_master_nasid);
796 	if (biosr)
797 		return -EINVAL;
798 
799 	biosr = uv_bios_get_heapsize((u64)uv_master_nasid, (u64)sizeof(u64), &v);
800 	if (biosr)
801 		return -EINVAL;
802 
803 	uv_biosheap = vmalloc(v);
804 	if (!uv_biosheap)
805 		return -ENOMEM;
806 
807 	biosr = uv_bios_install_heap((u64)uv_master_nasid, v, (u64 *)uv_biosheap);
808 	if (biosr) {
809 		vfree(uv_biosheap);
810 		return -EINVAL;
811 	}
812 
813 	biosr = uv_bios_obj_count((u64)uv_master_nasid, sizeof(u64), &v);
814 	if (biosr) {
815 		vfree(uv_biosheap);
816 		return -EINVAL;
817 	}
818 	uv_bios_obj_cnt = (int)v;
819 
820 	return 0;
821 }
822 
823 static struct attribute *hubless_base_attrs[] = {
824 	&partition_id_attr.attr,
825 	&uv_type_attr.attr,
826 	&uv_archtype_attr.attr,
827 	&uv_hubless_attr.attr,
828 	NULL,
829 };
830 
831 static const struct attribute_group hubless_base_attr_group = {
832 	.attrs = hubless_base_attrs
833 };
834 
835 
836 static int __init uv_sysfs_hubless_init(void)
837 {
838 	int ret;
839 
840 	ret = sysfs_create_group(sgi_uv_kobj, &hubless_base_attr_group);
841 	if (ret) {
842 		pr_warn("sysfs_create_group hubless_base_attr_group failed\n");
843 		kobject_put(sgi_uv_kobj);
844 	}
845 	return ret;
846 }
847 
848 static int __init uv_sysfs_init(void)
849 {
850 	int ret = 0;
851 
852 	if (!is_uv_system() && !uv_get_hubless_system())
853 		return -ENODEV;
854 
855 	num_cnodes = uv_num_possible_blades();
856 
857 	if (!sgi_uv_kobj)
858 		sgi_uv_kobj = kobject_create_and_add("sgi_uv", firmware_kobj);
859 	if (!sgi_uv_kobj) {
860 		pr_warn("kobject_create_and_add sgi_uv failed\n");
861 		return -EINVAL;
862 	}
863 
864 	if (uv_get_hubless_system())
865 		return uv_sysfs_hubless_init();
866 
867 	ret = sysfs_create_group(sgi_uv_kobj, &base_attr_group);
868 	if (ret) {
869 		pr_warn("sysfs_create_group base_attr_group failed\n");
870 		goto err_create_group;
871 	}
872 
873 	ret = initial_bios_setup();
874 	if (ret)
875 		goto err_bios_setup;
876 
877 	ret = uv_hubs_init();
878 	if (ret)
879 		goto err_hubs_init;
880 
881 	ret = uv_ports_init();
882 	if (ret)
883 		goto err_ports_init;
884 
885 	ret = pci_topology_init();
886 	if (ret)
887 		goto err_pci_init;
888 
889 	return 0;
890 
891 err_pci_init:
892 	uv_ports_exit();
893 err_ports_init:
894 	uv_hubs_exit();
895 err_hubs_init:
896 	vfree(uv_biosheap);
897 err_bios_setup:
898 	sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
899 err_create_group:
900 	kobject_put(sgi_uv_kobj);
901 	return ret;
902 }
903 
904 static void __exit uv_sysfs_hubless_exit(void)
905 {
906 	sysfs_remove_group(sgi_uv_kobj, &hubless_base_attr_group);
907 	kobject_put(sgi_uv_kobj);
908 }
909 
910 static void __exit uv_sysfs_exit(void)
911 {
912 	if (!is_uv_system()) {
913 		if (uv_get_hubless_system())
914 			uv_sysfs_hubless_exit();
915 		return;
916 	}
917 
918 	pci_topology_exit();
919 	uv_ports_exit();
920 	uv_hubs_exit();
921 	vfree(uv_biosheap);
922 	sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
923 	kobject_put(sgi_uv_kobj);
924 }
925 
926 #ifndef MODULE
927 device_initcall(uv_sysfs_init);
928 #else
929 module_init(uv_sysfs_init);
930 #endif
931 module_exit(uv_sysfs_exit);
932 
933 MODULE_AUTHOR("Hewlett Packard Enterprise");
934 MODULE_DESCRIPTION("Sysfs structure for HPE UV systems");
935 MODULE_LICENSE("GPL");
936