Linux Audio

Check our new training course

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