Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
  3 * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
  4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  5 *
  6 * This software is available to you under a choice of one of two
  7 * licenses.  You may choose to be licensed under the terms of the GNU
  8 * General Public License (GPL) Version 2, available from the file
  9 * COPYING in the main directory of this source tree, or the
 10 * OpenIB.org BSD license below:
 11 *
 12 *     Redistribution and use in source and binary forms, with or
 13 *     without modification, are permitted provided that the following
 14 *     conditions are met:
 15 *
 16 *      - Redistributions of source code must retain the above
 17 *        copyright notice, this list of conditions and the following
 18 *        disclaimer.
 19 *
 20 *      - Redistributions in binary form must reproduce the above
 21 *        copyright notice, this list of conditions and the following
 22 *        disclaimer in the documentation and/or other materials
 23 *        provided with the distribution.
 24 *
 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 32 * SOFTWARE.
 33 */
 34
 35#include "core_priv.h"
 36
 37#include <linux/slab.h>
 
 38#include <linux/string.h>
 
 39
 40#include <rdma/ib_mad.h>
 
 41
 
 
 
 
 
 
 
 
 42struct ib_port {
 43	struct kobject         kobj;
 44	struct ib_device      *ibdev;
 
 45	struct attribute_group gid_group;
 46	struct attribute_group pkey_group;
 47	u8                     port_num;
 
 48};
 49
 50struct port_attribute {
 51	struct attribute attr;
 52	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
 53	ssize_t (*store)(struct ib_port *, struct port_attribute *,
 54			 const char *buf, size_t count);
 55};
 56
 57#define PORT_ATTR(_name, _mode, _show, _store) \
 58struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
 59
 60#define PORT_ATTR_RO(_name) \
 61struct port_attribute port_attr_##_name = __ATTR_RO(_name)
 62
 63struct port_table_attribute {
 64	struct port_attribute	attr;
 65	char			name[8];
 66	int			index;
 
 67};
 68
 69static ssize_t port_attr_show(struct kobject *kobj,
 70			      struct attribute *attr, char *buf)
 71{
 72	struct port_attribute *port_attr =
 73		container_of(attr, struct port_attribute, attr);
 74	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
 75
 76	if (!port_attr->show)
 77		return -EIO;
 78
 79	return port_attr->show(p, port_attr, buf);
 80}
 81
 82static const struct sysfs_ops port_sysfs_ops = {
 83	.show = port_attr_show
 84};
 85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
 87			  char *buf)
 88{
 89	struct ib_port_attr attr;
 90	ssize_t ret;
 91
 92	static const char *state_name[] = {
 93		[IB_PORT_NOP]		= "NOP",
 94		[IB_PORT_DOWN]		= "DOWN",
 95		[IB_PORT_INIT]		= "INIT",
 96		[IB_PORT_ARMED]		= "ARMED",
 97		[IB_PORT_ACTIVE]	= "ACTIVE",
 98		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
 99	};
100
101	ret = ib_query_port(p->ibdev, p->port_num, &attr);
102	if (ret)
103		return ret;
104
105	return sprintf(buf, "%d: %s\n", attr.state,
106		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
107		       state_name[attr.state] : "UNKNOWN");
108}
109
110static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
111			char *buf)
112{
113	struct ib_port_attr attr;
114	ssize_t ret;
115
116	ret = ib_query_port(p->ibdev, p->port_num, &attr);
117	if (ret)
118		return ret;
119
120	return sprintf(buf, "0x%x\n", attr.lid);
121}
122
123static ssize_t lid_mask_count_show(struct ib_port *p,
124				   struct port_attribute *unused,
125				   char *buf)
126{
127	struct ib_port_attr attr;
128	ssize_t ret;
129
130	ret = ib_query_port(p->ibdev, p->port_num, &attr);
131	if (ret)
132		return ret;
133
134	return sprintf(buf, "%d\n", attr.lmc);
135}
136
137static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
138			   char *buf)
139{
140	struct ib_port_attr attr;
141	ssize_t ret;
142
143	ret = ib_query_port(p->ibdev, p->port_num, &attr);
144	if (ret)
145		return ret;
146
147	return sprintf(buf, "0x%x\n", attr.sm_lid);
148}
149
150static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
151			  char *buf)
152{
153	struct ib_port_attr attr;
154	ssize_t ret;
155
156	ret = ib_query_port(p->ibdev, p->port_num, &attr);
157	if (ret)
158		return ret;
159
160	return sprintf(buf, "%d\n", attr.sm_sl);
161}
162
163static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
164			     char *buf)
165{
166	struct ib_port_attr attr;
167	ssize_t ret;
168
169	ret = ib_query_port(p->ibdev, p->port_num, &attr);
170	if (ret)
171		return ret;
172
173	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
174}
175
176static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
177			 char *buf)
178{
179	struct ib_port_attr attr;
180	char *speed = "";
181	int rate;
182	ssize_t ret;
183
184	ret = ib_query_port(p->ibdev, p->port_num, &attr);
185	if (ret)
186		return ret;
187
188	switch (attr.active_speed) {
189	case 2: speed = " DDR"; break;
190	case 4: speed = " QDR"; break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191	}
192
193	rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
194	if (rate < 0)
195		return -EINVAL;
196
197	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
198		       rate / 10, rate % 10 ? ".5" : "",
199		       ib_width_enum_to_int(attr.active_width), speed);
200}
201
202static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
203			       char *buf)
204{
205	struct ib_port_attr attr;
206
207	ssize_t ret;
208
209	ret = ib_query_port(p->ibdev, p->port_num, &attr);
210	if (ret)
211		return ret;
212
213	switch (attr.phys_state) {
214	case 1:  return sprintf(buf, "1: Sleep\n");
215	case 2:  return sprintf(buf, "2: Polling\n");
216	case 3:  return sprintf(buf, "3: Disabled\n");
217	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
218	case 5:  return sprintf(buf, "5: LinkUp\n");
219	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
220	case 7:  return sprintf(buf, "7: Phy Test\n");
221	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
222	}
223}
224
225static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
226			       char *buf)
227{
228	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
229	case IB_LINK_LAYER_INFINIBAND:
230		return sprintf(buf, "%s\n", "InfiniBand");
231	case IB_LINK_LAYER_ETHERNET:
232		return sprintf(buf, "%s\n", "Ethernet");
233	default:
234		return sprintf(buf, "%s\n", "Unknown");
235	}
236}
237
238static PORT_ATTR_RO(state);
239static PORT_ATTR_RO(lid);
240static PORT_ATTR_RO(lid_mask_count);
241static PORT_ATTR_RO(sm_lid);
242static PORT_ATTR_RO(sm_sl);
243static PORT_ATTR_RO(cap_mask);
244static PORT_ATTR_RO(rate);
245static PORT_ATTR_RO(phys_state);
246static PORT_ATTR_RO(link_layer);
247
248static struct attribute *port_default_attrs[] = {
249	&port_attr_state.attr,
250	&port_attr_lid.attr,
251	&port_attr_lid_mask_count.attr,
252	&port_attr_sm_lid.attr,
253	&port_attr_sm_sl.attr,
254	&port_attr_cap_mask.attr,
255	&port_attr_rate.attr,
256	&port_attr_phys_state.attr,
257	&port_attr_link_layer.attr,
258	NULL
259};
260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
262			     char *buf)
263{
264	struct port_table_attribute *tab_attr =
265		container_of(attr, struct port_table_attribute, attr);
266	union ib_gid gid;
267	ssize_t ret;
268
269	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
270	if (ret)
271		return ret;
272
273	return sprintf(buf, "%pI6\n", gid.raw);
274}
275
 
 
 
 
 
 
 
 
 
 
 
 
 
276static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
277			      char *buf)
278{
279	struct port_table_attribute *tab_attr =
280		container_of(attr, struct port_table_attribute, attr);
281	u16 pkey;
282	ssize_t ret;
283
284	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
285	if (ret)
286		return ret;
287
288	return sprintf(buf, "0x%04x\n", pkey);
289}
290
291#define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
292struct port_table_attribute port_pma_attr_##_name = {			\
293	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
294	.index = (_offset) | ((_width) << 16) | ((_counter) << 24)	\
 
295}
296
297static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
298				char *buf)
299{
300	struct port_table_attribute *tab_attr =
301		container_of(attr, struct port_table_attribute, attr);
302	int offset = tab_attr->index & 0xffff;
303	int width  = (tab_attr->index >> 16) & 0xff;
304	struct ib_mad *in_mad  = NULL;
305	struct ib_mad *out_mad = NULL;
 
 
 
 
 
 
 
 
 
306	ssize_t ret;
307
308	if (!p->ibdev->process_mad)
309		return sprintf(buf, "N/A (no PMA)\n");
310
311	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
312	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
313	if (!in_mad || !out_mad) {
314		ret = -ENOMEM;
315		goto out;
316	}
317
318	in_mad->mad_hdr.base_version  = 1;
319	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
320	in_mad->mad_hdr.class_version = 1;
321	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
322	in_mad->mad_hdr.attr_id       = cpu_to_be16(0x12); /* PortCounters */
323
324	in_mad->data[41] = p->port_num;	/* PortSelect field */
 
325
326	if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
327		 p->port_num, NULL, NULL, in_mad, out_mad) &
 
 
 
328	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
329	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
330		ret = -EINVAL;
331		goto out;
332	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
334	switch (width) {
335	case 4:
336		ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
337					    (4 - (offset % 8))) & 0xf);
338		break;
339	case 8:
340		ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
341		break;
342	case 16:
343		ret = sprintf(buf, "%u\n",
344			      be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
345		break;
346	case 32:
347		ret = sprintf(buf, "%u\n",
348			      be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
 
 
 
 
349		break;
 
350	default:
351		ret = 0;
352	}
353
354out:
355	kfree(in_mad);
356	kfree(out_mad);
357
358	return ret;
359}
360
361static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
362static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
363static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
364static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
365static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
366static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
367static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
368static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
369static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
370static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
371static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
372static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
373static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
374static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
375static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
376static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
377
 
 
 
 
 
 
 
 
 
 
 
 
378static struct attribute *pma_attrs[] = {
379	&port_pma_attr_symbol_error.attr.attr,
380	&port_pma_attr_link_error_recovery.attr.attr,
381	&port_pma_attr_link_downed.attr.attr,
382	&port_pma_attr_port_rcv_errors.attr.attr,
383	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
384	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
385	&port_pma_attr_port_xmit_discards.attr.attr,
386	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
387	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
388	&port_pma_attr_local_link_integrity_errors.attr.attr,
389	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
390	&port_pma_attr_VL15_dropped.attr.attr,
391	&port_pma_attr_port_xmit_data.attr.attr,
392	&port_pma_attr_port_rcv_data.attr.attr,
393	&port_pma_attr_port_xmit_packets.attr.attr,
394	&port_pma_attr_port_rcv_packets.attr.attr,
395	NULL
396};
397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398static struct attribute_group pma_group = {
399	.name  = "counters",
400	.attrs  = pma_attrs
401};
402
 
 
 
 
 
 
 
 
 
 
403static void ib_port_release(struct kobject *kobj)
404{
405	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
406	struct attribute *a;
407	int i;
408
409	for (i = 0; (a = p->gid_group.attrs[i]); ++i)
410		kfree(a);
 
411
412	kfree(p->gid_group.attrs);
 
413
414	for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
415		kfree(a);
 
416
417	kfree(p->pkey_group.attrs);
 
418
419	kfree(p);
420}
421
422static struct kobj_type port_type = {
423	.release       = ib_port_release,
424	.sysfs_ops     = &port_sysfs_ops,
425	.default_attrs = port_default_attrs
426};
427
428static void ib_device_release(struct device *device)
429{
430	struct ib_device *dev = container_of(device, struct ib_device, dev);
 
 
 
431
432	kfree(dev);
433}
 
434
435static int ib_device_uevent(struct device *device,
436			    struct kobj_uevent_env *env)
437{
438	struct ib_device *dev = container_of(device, struct ib_device, dev);
439
440	if (add_uevent_var(env, "NAME=%s", dev->name))
441		return -ENOMEM;
 
442
443	/*
444	 * It would be nice to pass the node GUID with the event...
445	 */
446
447	return 0;
448}
449
 
 
 
 
 
 
 
 
 
 
 
450static struct attribute **
451alloc_group_attrs(ssize_t (*show)(struct ib_port *,
452				  struct port_attribute *, char *buf),
453		  int len)
454{
455	struct attribute **tab_attr;
456	struct port_table_attribute *element;
457	int i;
458
459	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
460	if (!tab_attr)
461		return NULL;
462
463	for (i = 0; i < len; i++) {
464		element = kzalloc(sizeof(struct port_table_attribute),
465				  GFP_KERNEL);
466		if (!element)
467			goto err;
468
469		if (snprintf(element->name, sizeof(element->name),
470			     "%d", i) >= sizeof(element->name)) {
471			kfree(element);
472			goto err;
473		}
474
475		element->attr.attr.name  = element->name;
476		element->attr.attr.mode  = S_IRUGO;
477		element->attr.show       = show;
478		element->index		 = i;
479		sysfs_attr_init(&element->attr.attr);
480
481		tab_attr[i] = &element->attr.attr;
482	}
483
484	return tab_attr;
485
486err:
487	while (--i >= 0)
488		kfree(tab_attr[i]);
489	kfree(tab_attr);
490	return NULL;
491}
492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493static int add_port(struct ib_device *device, int port_num,
494		    int (*port_callback)(struct ib_device *,
495					 u8, struct kobject *))
496{
497	struct ib_port *p;
498	struct ib_port_attr attr;
499	int i;
500	int ret;
501
502	ret = ib_query_port(device, port_num, &attr);
503	if (ret)
504		return ret;
505
506	p = kzalloc(sizeof *p, GFP_KERNEL);
507	if (!p)
508		return -ENOMEM;
509
510	p->ibdev      = device;
511	p->port_num   = port_num;
512
513	ret = kobject_init_and_add(&p->kobj, &port_type,
514				   kobject_get(device->ports_parent),
515				   "%d", port_num);
516	if (ret)
 
 
 
 
 
 
 
517		goto err_put;
 
518
519	ret = sysfs_create_group(&p->kobj, &pma_group);
520	if (ret)
 
 
 
521		goto err_put;
 
 
 
 
 
 
522
523	p->gid_group.name  = "gids";
524	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
525	if (!p->gid_group.attrs)
 
526		goto err_remove_pma;
 
527
528	ret = sysfs_create_group(&p->kobj, &p->gid_group);
529	if (ret)
530		goto err_free_gid;
531
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532	p->pkey_group.name  = "pkeys";
533	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
534						attr.pkey_tbl_len);
535	if (!p->pkey_group.attrs)
536		goto err_remove_gid;
 
 
537
538	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
539	if (ret)
540		goto err_free_pkey;
541
542	if (port_callback) {
543		ret = port_callback(device, port_num, &p->kobj);
544		if (ret)
545			goto err_remove_pkey;
546	}
547
548	list_add_tail(&p->kobj.entry, &device->port_list);
549
550	kobject_uevent(&p->kobj, KOBJ_ADD);
551	return 0;
552
553err_remove_pkey:
554	sysfs_remove_group(&p->kobj, &p->pkey_group);
555
556err_free_pkey:
557	for (i = 0; i < attr.pkey_tbl_len; ++i)
558		kfree(p->pkey_group.attrs[i]);
559
560	kfree(p->pkey_group.attrs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561
562err_remove_gid:
563	sysfs_remove_group(&p->kobj, &p->gid_group);
564
565err_free_gid:
566	for (i = 0; i < attr.gid_tbl_len; ++i)
567		kfree(p->gid_group.attrs[i]);
568
569	kfree(p->gid_group.attrs);
 
570
571err_remove_pma:
572	sysfs_remove_group(&p->kobj, &pma_group);
 
 
 
573
574err_put:
575	kobject_put(device->ports_parent);
576	kfree(p);
577	return ret;
578}
579
580static ssize_t show_node_type(struct device *device,
581			      struct device_attribute *attr, char *buf)
582{
583	struct ib_device *dev = container_of(device, struct ib_device, dev);
584
585	switch (dev->node_type) {
586	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
587	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
 
 
588	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
589	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
590	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
591	}
592}
593
594static ssize_t show_sys_image_guid(struct device *device,
595				   struct device_attribute *dev_attr, char *buf)
596{
597	struct ib_device *dev = container_of(device, struct ib_device, dev);
598	struct ib_device_attr attr;
599	ssize_t ret;
600
601	ret = ib_query_device(dev, &attr);
602	if (ret)
603		return ret;
604
605	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
606		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
607		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
608		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
609		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
610}
611
612static ssize_t show_node_guid(struct device *device,
613			      struct device_attribute *attr, char *buf)
614{
615	struct ib_device *dev = container_of(device, struct ib_device, dev);
616
617	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
618		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
619		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
620		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
621		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
622}
623
624static ssize_t show_node_desc(struct device *device,
625			      struct device_attribute *attr, char *buf)
626{
627	struct ib_device *dev = container_of(device, struct ib_device, dev);
628
629	return sprintf(buf, "%.64s\n", dev->node_desc);
630}
631
632static ssize_t set_node_desc(struct device *device,
633			     struct device_attribute *attr,
634			     const char *buf, size_t count)
635{
636	struct ib_device *dev = container_of(device, struct ib_device, dev);
637	struct ib_device_modify desc = {};
638	int ret;
639
640	if (!dev->modify_device)
641		return -EIO;
642
643	memcpy(desc.node_desc, buf, min_t(int, count, 64));
644	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
645	if (ret)
646		return ret;
647
648	return count;
649}
650
651static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
652static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
653static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
654static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
655
656static struct device_attribute *ib_class_attributes[] = {
657	&dev_attr_node_type,
658	&dev_attr_sys_image_guid,
659	&dev_attr_node_guid,
660	&dev_attr_node_desc
661};
662
663static struct class ib_class = {
664	.name    = "infiniband",
665	.dev_release = ib_device_release,
666	.dev_uevent = ib_device_uevent,
667};
668
669/* Show a given an attribute in the statistics group */
670static ssize_t show_protocol_stat(const struct device *device,
671			    struct device_attribute *attr, char *buf,
672			    unsigned offset)
673{
674	struct ib_device *dev = container_of(device, struct ib_device, dev);
675	union rdma_protocol_stats stats;
676	ssize_t ret;
677
678	ret = dev->get_protocol_stats(dev, &stats);
679	if (ret)
680		return ret;
681
682	return sprintf(buf, "%llu\n",
683		       (unsigned long long) ((u64 *) &stats)[offset]);
684}
685
686/* generate a read-only iwarp statistics attribute */
687#define IW_STATS_ENTRY(name)						\
688static ssize_t show_##name(struct device *device,			\
689			   struct device_attribute *attr, char *buf)	\
690{									\
691	return show_protocol_stat(device, attr, buf,			\
692				  offsetof(struct iw_protocol_stats, name) / \
693				  sizeof (u64));			\
694}									\
695static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
696
697IW_STATS_ENTRY(ipInReceives);
698IW_STATS_ENTRY(ipInHdrErrors);
699IW_STATS_ENTRY(ipInTooBigErrors);
700IW_STATS_ENTRY(ipInNoRoutes);
701IW_STATS_ENTRY(ipInAddrErrors);
702IW_STATS_ENTRY(ipInUnknownProtos);
703IW_STATS_ENTRY(ipInTruncatedPkts);
704IW_STATS_ENTRY(ipInDiscards);
705IW_STATS_ENTRY(ipInDelivers);
706IW_STATS_ENTRY(ipOutForwDatagrams);
707IW_STATS_ENTRY(ipOutRequests);
708IW_STATS_ENTRY(ipOutDiscards);
709IW_STATS_ENTRY(ipOutNoRoutes);
710IW_STATS_ENTRY(ipReasmTimeout);
711IW_STATS_ENTRY(ipReasmReqds);
712IW_STATS_ENTRY(ipReasmOKs);
713IW_STATS_ENTRY(ipReasmFails);
714IW_STATS_ENTRY(ipFragOKs);
715IW_STATS_ENTRY(ipFragFails);
716IW_STATS_ENTRY(ipFragCreates);
717IW_STATS_ENTRY(ipInMcastPkts);
718IW_STATS_ENTRY(ipOutMcastPkts);
719IW_STATS_ENTRY(ipInBcastPkts);
720IW_STATS_ENTRY(ipOutBcastPkts);
721IW_STATS_ENTRY(tcpRtoAlgorithm);
722IW_STATS_ENTRY(tcpRtoMin);
723IW_STATS_ENTRY(tcpRtoMax);
724IW_STATS_ENTRY(tcpMaxConn);
725IW_STATS_ENTRY(tcpActiveOpens);
726IW_STATS_ENTRY(tcpPassiveOpens);
727IW_STATS_ENTRY(tcpAttemptFails);
728IW_STATS_ENTRY(tcpEstabResets);
729IW_STATS_ENTRY(tcpCurrEstab);
730IW_STATS_ENTRY(tcpInSegs);
731IW_STATS_ENTRY(tcpOutSegs);
732IW_STATS_ENTRY(tcpRetransSegs);
733IW_STATS_ENTRY(tcpInErrs);
734IW_STATS_ENTRY(tcpOutRsts);
735
736static struct attribute *iw_proto_stats_attrs[] = {
737	&dev_attr_ipInReceives.attr,
738	&dev_attr_ipInHdrErrors.attr,
739	&dev_attr_ipInTooBigErrors.attr,
740	&dev_attr_ipInNoRoutes.attr,
741	&dev_attr_ipInAddrErrors.attr,
742	&dev_attr_ipInUnknownProtos.attr,
743	&dev_attr_ipInTruncatedPkts.attr,
744	&dev_attr_ipInDiscards.attr,
745	&dev_attr_ipInDelivers.attr,
746	&dev_attr_ipOutForwDatagrams.attr,
747	&dev_attr_ipOutRequests.attr,
748	&dev_attr_ipOutDiscards.attr,
749	&dev_attr_ipOutNoRoutes.attr,
750	&dev_attr_ipReasmTimeout.attr,
751	&dev_attr_ipReasmReqds.attr,
752	&dev_attr_ipReasmOKs.attr,
753	&dev_attr_ipReasmFails.attr,
754	&dev_attr_ipFragOKs.attr,
755	&dev_attr_ipFragFails.attr,
756	&dev_attr_ipFragCreates.attr,
757	&dev_attr_ipInMcastPkts.attr,
758	&dev_attr_ipOutMcastPkts.attr,
759	&dev_attr_ipInBcastPkts.attr,
760	&dev_attr_ipOutBcastPkts.attr,
761	&dev_attr_tcpRtoAlgorithm.attr,
762	&dev_attr_tcpRtoMin.attr,
763	&dev_attr_tcpRtoMax.attr,
764	&dev_attr_tcpMaxConn.attr,
765	&dev_attr_tcpActiveOpens.attr,
766	&dev_attr_tcpPassiveOpens.attr,
767	&dev_attr_tcpAttemptFails.attr,
768	&dev_attr_tcpEstabResets.attr,
769	&dev_attr_tcpCurrEstab.attr,
770	&dev_attr_tcpInSegs.attr,
771	&dev_attr_tcpOutSegs.attr,
772	&dev_attr_tcpRetransSegs.attr,
773	&dev_attr_tcpInErrs.attr,
774	&dev_attr_tcpOutRsts.attr,
775	NULL
776};
777
778static struct attribute_group iw_stats_group = {
779	.name	= "proto_stats",
780	.attrs	= iw_proto_stats_attrs,
781};
782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783int ib_device_register_sysfs(struct ib_device *device,
784			     int (*port_callback)(struct ib_device *,
785						  u8, struct kobject *))
786{
787	struct device *class_dev = &device->dev;
788	int ret;
789	int i;
790
791	class_dev->class      = &ib_class;
792	class_dev->parent     = device->dma_device;
793	dev_set_name(class_dev, device->name);
794	dev_set_drvdata(class_dev, device);
795
796	INIT_LIST_HEAD(&device->port_list);
797
798	ret = device_register(class_dev);
799	if (ret)
800		goto err;
801
802	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
803		ret = device_create_file(class_dev, ib_class_attributes[i]);
804		if (ret)
805			goto err_unregister;
806	}
807
808	device->ports_parent = kobject_create_and_add("ports",
809					kobject_get(&class_dev->kobj));
810	if (!device->ports_parent) {
811		ret = -ENOMEM;
812		goto err_put;
813	}
814
815	if (device->node_type == RDMA_NODE_IB_SWITCH) {
816		ret = add_port(device, 0, port_callback);
817		if (ret)
818			goto err_put;
819	} else {
820		for (i = 1; i <= device->phys_port_cnt; ++i) {
821			ret = add_port(device, i, port_callback);
822			if (ret)
823				goto err_put;
824		}
825	}
826
827	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
828		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
829		if (ret)
830			goto err_put;
831	}
832
833	return 0;
834
835err_put:
836	{
837		struct kobject *p, *t;
838		struct ib_port *port;
839
840		list_for_each_entry_safe(p, t, &device->port_list, entry) {
841			list_del(&p->entry);
842			port = container_of(p, struct ib_port, kobj);
843			sysfs_remove_group(p, &pma_group);
844			sysfs_remove_group(p, &port->pkey_group);
845			sysfs_remove_group(p, &port->gid_group);
846			kobject_put(p);
847		}
848	}
849
850	kobject_put(&class_dev->kobj);
851
852err_unregister:
853	device_unregister(class_dev);
854
855err:
856	return ret;
857}
858
859void ib_device_unregister_sysfs(struct ib_device *device)
860{
861	struct kobject *p, *t;
862	struct ib_port *port;
863
864	/* Hold kobject until ib_dealloc_device() */
865	kobject_get(&device->dev.kobj);
 
866
867	list_for_each_entry_safe(p, t, &device->port_list, entry) {
868		list_del(&p->entry);
869		port = container_of(p, struct ib_port, kobj);
870		sysfs_remove_group(p, &pma_group);
871		sysfs_remove_group(p, &port->pkey_group);
872		sysfs_remove_group(p, &port->gid_group);
873		kobject_put(p);
874	}
875
876	kobject_put(device->ports_parent);
877	device_unregister(&device->dev);
878}
879
880int ib_sysfs_setup(void)
881{
882	return class_register(&ib_class);
883}
884
885void ib_sysfs_cleanup(void)
886{
887	class_unregister(&ib_class);
888}
v4.6
   1/*
   2 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
   3 * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
   4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
   5 *
   6 * This software is available to you under a choice of one of two
   7 * licenses.  You may choose to be licensed under the terms of the GNU
   8 * General Public License (GPL) Version 2, available from the file
   9 * COPYING in the main directory of this source tree, or the
  10 * OpenIB.org BSD license below:
  11 *
  12 *     Redistribution and use in source and binary forms, with or
  13 *     without modification, are permitted provided that the following
  14 *     conditions are met:
  15 *
  16 *      - Redistributions of source code must retain the above
  17 *        copyright notice, this list of conditions and the following
  18 *        disclaimer.
  19 *
  20 *      - Redistributions in binary form must reproduce the above
  21 *        copyright notice, this list of conditions and the following
  22 *        disclaimer in the documentation and/or other materials
  23 *        provided with the distribution.
  24 *
  25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32 * SOFTWARE.
  33 */
  34
  35#include "core_priv.h"
  36
  37#include <linux/slab.h>
  38#include <linux/stat.h>
  39#include <linux/string.h>
  40#include <linux/netdevice.h>
  41
  42#include <rdma/ib_mad.h>
  43#include <rdma/ib_pma.h>
  44
  45struct ib_port;
  46
  47struct gid_attr_group {
  48	struct ib_port		*port;
  49	struct kobject		kobj;
  50	struct attribute_group	ndev;
  51	struct attribute_group	type;
  52};
  53struct ib_port {
  54	struct kobject         kobj;
  55	struct ib_device      *ibdev;
  56	struct gid_attr_group *gid_attr_group;
  57	struct attribute_group gid_group;
  58	struct attribute_group pkey_group;
  59	u8                     port_num;
  60	struct attribute_group *pma_table;
  61};
  62
  63struct port_attribute {
  64	struct attribute attr;
  65	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
  66	ssize_t (*store)(struct ib_port *, struct port_attribute *,
  67			 const char *buf, size_t count);
  68};
  69
  70#define PORT_ATTR(_name, _mode, _show, _store) \
  71struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
  72
  73#define PORT_ATTR_RO(_name) \
  74struct port_attribute port_attr_##_name = __ATTR_RO(_name)
  75
  76struct port_table_attribute {
  77	struct port_attribute	attr;
  78	char			name[8];
  79	int			index;
  80	__be16			attr_id;
  81};
  82
  83static ssize_t port_attr_show(struct kobject *kobj,
  84			      struct attribute *attr, char *buf)
  85{
  86	struct port_attribute *port_attr =
  87		container_of(attr, struct port_attribute, attr);
  88	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  89
  90	if (!port_attr->show)
  91		return -EIO;
  92
  93	return port_attr->show(p, port_attr, buf);
  94}
  95
  96static const struct sysfs_ops port_sysfs_ops = {
  97	.show = port_attr_show
  98};
  99
 100static ssize_t gid_attr_show(struct kobject *kobj,
 101			     struct attribute *attr, char *buf)
 102{
 103	struct port_attribute *port_attr =
 104		container_of(attr, struct port_attribute, attr);
 105	struct ib_port *p = container_of(kobj, struct gid_attr_group,
 106					 kobj)->port;
 107
 108	if (!port_attr->show)
 109		return -EIO;
 110
 111	return port_attr->show(p, port_attr, buf);
 112}
 113
 114static const struct sysfs_ops gid_attr_sysfs_ops = {
 115	.show = gid_attr_show
 116};
 117
 118static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
 119			  char *buf)
 120{
 121	struct ib_port_attr attr;
 122	ssize_t ret;
 123
 124	static const char *state_name[] = {
 125		[IB_PORT_NOP]		= "NOP",
 126		[IB_PORT_DOWN]		= "DOWN",
 127		[IB_PORT_INIT]		= "INIT",
 128		[IB_PORT_ARMED]		= "ARMED",
 129		[IB_PORT_ACTIVE]	= "ACTIVE",
 130		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
 131	};
 132
 133	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 134	if (ret)
 135		return ret;
 136
 137	return sprintf(buf, "%d: %s\n", attr.state,
 138		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
 139		       state_name[attr.state] : "UNKNOWN");
 140}
 141
 142static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
 143			char *buf)
 144{
 145	struct ib_port_attr attr;
 146	ssize_t ret;
 147
 148	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 149	if (ret)
 150		return ret;
 151
 152	return sprintf(buf, "0x%x\n", attr.lid);
 153}
 154
 155static ssize_t lid_mask_count_show(struct ib_port *p,
 156				   struct port_attribute *unused,
 157				   char *buf)
 158{
 159	struct ib_port_attr attr;
 160	ssize_t ret;
 161
 162	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 163	if (ret)
 164		return ret;
 165
 166	return sprintf(buf, "%d\n", attr.lmc);
 167}
 168
 169static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
 170			   char *buf)
 171{
 172	struct ib_port_attr attr;
 173	ssize_t ret;
 174
 175	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 176	if (ret)
 177		return ret;
 178
 179	return sprintf(buf, "0x%x\n", attr.sm_lid);
 180}
 181
 182static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
 183			  char *buf)
 184{
 185	struct ib_port_attr attr;
 186	ssize_t ret;
 187
 188	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 189	if (ret)
 190		return ret;
 191
 192	return sprintf(buf, "%d\n", attr.sm_sl);
 193}
 194
 195static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
 196			     char *buf)
 197{
 198	struct ib_port_attr attr;
 199	ssize_t ret;
 200
 201	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 202	if (ret)
 203		return ret;
 204
 205	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
 206}
 207
 208static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
 209			 char *buf)
 210{
 211	struct ib_port_attr attr;
 212	char *speed = "";
 213	int rate;		/* in deci-Gb/sec */
 214	ssize_t ret;
 215
 216	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 217	if (ret)
 218		return ret;
 219
 220	switch (attr.active_speed) {
 221	case IB_SPEED_DDR:
 222		speed = " DDR";
 223		rate = 50;
 224		break;
 225	case IB_SPEED_QDR:
 226		speed = " QDR";
 227		rate = 100;
 228		break;
 229	case IB_SPEED_FDR10:
 230		speed = " FDR10";
 231		rate = 100;
 232		break;
 233	case IB_SPEED_FDR:
 234		speed = " FDR";
 235		rate = 140;
 236		break;
 237	case IB_SPEED_EDR:
 238		speed = " EDR";
 239		rate = 250;
 240		break;
 241	case IB_SPEED_SDR:
 242	default:		/* default to SDR for invalid rates */
 243		rate = 25;
 244		break;
 245	}
 246
 247	rate *= ib_width_enum_to_int(attr.active_width);
 248	if (rate < 0)
 249		return -EINVAL;
 250
 251	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
 252		       rate / 10, rate % 10 ? ".5" : "",
 253		       ib_width_enum_to_int(attr.active_width), speed);
 254}
 255
 256static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
 257			       char *buf)
 258{
 259	struct ib_port_attr attr;
 260
 261	ssize_t ret;
 262
 263	ret = ib_query_port(p->ibdev, p->port_num, &attr);
 264	if (ret)
 265		return ret;
 266
 267	switch (attr.phys_state) {
 268	case 1:  return sprintf(buf, "1: Sleep\n");
 269	case 2:  return sprintf(buf, "2: Polling\n");
 270	case 3:  return sprintf(buf, "3: Disabled\n");
 271	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
 272	case 5:  return sprintf(buf, "5: LinkUp\n");
 273	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
 274	case 7:  return sprintf(buf, "7: Phy Test\n");
 275	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
 276	}
 277}
 278
 279static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
 280			       char *buf)
 281{
 282	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
 283	case IB_LINK_LAYER_INFINIBAND:
 284		return sprintf(buf, "%s\n", "InfiniBand");
 285	case IB_LINK_LAYER_ETHERNET:
 286		return sprintf(buf, "%s\n", "Ethernet");
 287	default:
 288		return sprintf(buf, "%s\n", "Unknown");
 289	}
 290}
 291
 292static PORT_ATTR_RO(state);
 293static PORT_ATTR_RO(lid);
 294static PORT_ATTR_RO(lid_mask_count);
 295static PORT_ATTR_RO(sm_lid);
 296static PORT_ATTR_RO(sm_sl);
 297static PORT_ATTR_RO(cap_mask);
 298static PORT_ATTR_RO(rate);
 299static PORT_ATTR_RO(phys_state);
 300static PORT_ATTR_RO(link_layer);
 301
 302static struct attribute *port_default_attrs[] = {
 303	&port_attr_state.attr,
 304	&port_attr_lid.attr,
 305	&port_attr_lid_mask_count.attr,
 306	&port_attr_sm_lid.attr,
 307	&port_attr_sm_sl.attr,
 308	&port_attr_cap_mask.attr,
 309	&port_attr_rate.attr,
 310	&port_attr_phys_state.attr,
 311	&port_attr_link_layer.attr,
 312	NULL
 313};
 314
 315static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
 316{
 317	if (!gid_attr->ndev)
 318		return -EINVAL;
 319
 320	return sprintf(buf, "%s\n", gid_attr->ndev->name);
 321}
 322
 323static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
 324{
 325	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
 326}
 327
 328static ssize_t _show_port_gid_attr(struct ib_port *p,
 329				   struct port_attribute *attr,
 330				   char *buf,
 331				   size_t (*print)(struct ib_gid_attr *gid_attr,
 332						   char *buf))
 333{
 334	struct port_table_attribute *tab_attr =
 335		container_of(attr, struct port_table_attribute, attr);
 336	union ib_gid gid;
 337	struct ib_gid_attr gid_attr = {};
 338	ssize_t ret;
 339
 340	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
 341			   &gid_attr);
 342	if (ret)
 343		goto err;
 344
 345	ret = print(&gid_attr, buf);
 346
 347err:
 348	if (gid_attr.ndev)
 349		dev_put(gid_attr.ndev);
 350	return ret;
 351}
 352
 353static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
 354			     char *buf)
 355{
 356	struct port_table_attribute *tab_attr =
 357		container_of(attr, struct port_table_attribute, attr);
 358	union ib_gid gid;
 359	ssize_t ret;
 360
 361	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
 362	if (ret)
 363		return ret;
 364
 365	return sprintf(buf, "%pI6\n", gid.raw);
 366}
 367
 368static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
 369				       struct port_attribute *attr, char *buf)
 370{
 371	return _show_port_gid_attr(p, attr, buf, print_ndev);
 372}
 373
 374static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
 375					   struct port_attribute *attr,
 376					   char *buf)
 377{
 378	return _show_port_gid_attr(p, attr, buf, print_gid_type);
 379}
 380
 381static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
 382			      char *buf)
 383{
 384	struct port_table_attribute *tab_attr =
 385		container_of(attr, struct port_table_attribute, attr);
 386	u16 pkey;
 387	ssize_t ret;
 388
 389	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
 390	if (ret)
 391		return ret;
 392
 393	return sprintf(buf, "0x%04x\n", pkey);
 394}
 395
 396#define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
 397struct port_table_attribute port_pma_attr_##_name = {			\
 398	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
 399	.index = (_offset) | ((_width) << 16) | ((_counter) << 24),	\
 400	.attr_id = IB_PMA_PORT_COUNTERS ,				\
 401}
 402
 403#define PORT_PMA_ATTR_EXT(_name, _width, _offset)			\
 404struct port_table_attribute port_pma_attr_ext_##_name = {		\
 405	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
 406	.index = (_offset) | ((_width) << 16),				\
 407	.attr_id = IB_PMA_PORT_COUNTERS_EXT ,				\
 408}
 409
 410/*
 411 * Get a Perfmgmt MAD block of data.
 412 * Returns error code or the number of bytes retrieved.
 413 */
 414static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
 415		void *data, int offset, size_t size)
 416{
 417	struct ib_mad *in_mad;
 418	struct ib_mad *out_mad;
 419	size_t mad_size = sizeof(*out_mad);
 420	u16 out_mad_pkey_index = 0;
 421	ssize_t ret;
 422
 423	if (!dev->process_mad)
 424		return -ENOSYS;
 425
 426	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
 427	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
 428	if (!in_mad || !out_mad) {
 429		ret = -ENOMEM;
 430		goto out;
 431	}
 432
 433	in_mad->mad_hdr.base_version  = 1;
 434	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
 435	in_mad->mad_hdr.class_version = 1;
 436	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
 437	in_mad->mad_hdr.attr_id       = attr;
 438
 439	if (attr != IB_PMA_CLASS_PORT_INFO)
 440		in_mad->data[41] = port_num;	/* PortSelect field */
 441
 442	if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
 443		 port_num, NULL, NULL,
 444		 (const struct ib_mad_hdr *)in_mad, mad_size,
 445		 (struct ib_mad_hdr *)out_mad, &mad_size,
 446		 &out_mad_pkey_index) &
 447	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
 448	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
 449		ret = -EINVAL;
 450		goto out;
 451	}
 452	memcpy(data, out_mad->data + offset, size);
 453	ret = size;
 454out:
 455	kfree(in_mad);
 456	kfree(out_mad);
 457	return ret;
 458}
 459
 460static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
 461				char *buf)
 462{
 463	struct port_table_attribute *tab_attr =
 464		container_of(attr, struct port_table_attribute, attr);
 465	int offset = tab_attr->index & 0xffff;
 466	int width  = (tab_attr->index >> 16) & 0xff;
 467	ssize_t ret;
 468	u8 data[8];
 469
 470	ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
 471			40 + offset / 8, sizeof(data));
 472	if (ret < 0)
 473		return sprintf(buf, "N/A (no PMA)\n");
 474
 475	switch (width) {
 476	case 4:
 477		ret = sprintf(buf, "%u\n", (*data >>
 478					    (4 - (offset % 8))) & 0xf);
 479		break;
 480	case 8:
 481		ret = sprintf(buf, "%u\n", *data);
 482		break;
 483	case 16:
 484		ret = sprintf(buf, "%u\n",
 485			      be16_to_cpup((__be16 *)data));
 486		break;
 487	case 32:
 488		ret = sprintf(buf, "%u\n",
 489			      be32_to_cpup((__be32 *)data));
 490		break;
 491	case 64:
 492		ret = sprintf(buf, "%llu\n",
 493				be64_to_cpup((__be64 *)data));
 494		break;
 495
 496	default:
 497		ret = 0;
 498	}
 499
 
 
 
 
 500	return ret;
 501}
 502
 503static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
 504static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
 505static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
 506static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
 507static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
 508static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
 509static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
 510static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
 511static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
 512static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
 513static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
 514static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
 515static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
 516static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
 517static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
 518static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
 519
 520/*
 521 * Counters added by extended set
 522 */
 523static PORT_PMA_ATTR_EXT(port_xmit_data		    , 64,  64);
 524static PORT_PMA_ATTR_EXT(port_rcv_data		    , 64, 128);
 525static PORT_PMA_ATTR_EXT(port_xmit_packets	    , 64, 192);
 526static PORT_PMA_ATTR_EXT(port_rcv_packets	    , 64, 256);
 527static PORT_PMA_ATTR_EXT(unicast_xmit_packets	    , 64, 320);
 528static PORT_PMA_ATTR_EXT(unicast_rcv_packets	    , 64, 384);
 529static PORT_PMA_ATTR_EXT(multicast_xmit_packets	    , 64, 448);
 530static PORT_PMA_ATTR_EXT(multicast_rcv_packets	    , 64, 512);
 531
 532static struct attribute *pma_attrs[] = {
 533	&port_pma_attr_symbol_error.attr.attr,
 534	&port_pma_attr_link_error_recovery.attr.attr,
 535	&port_pma_attr_link_downed.attr.attr,
 536	&port_pma_attr_port_rcv_errors.attr.attr,
 537	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
 538	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
 539	&port_pma_attr_port_xmit_discards.attr.attr,
 540	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
 541	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
 542	&port_pma_attr_local_link_integrity_errors.attr.attr,
 543	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
 544	&port_pma_attr_VL15_dropped.attr.attr,
 545	&port_pma_attr_port_xmit_data.attr.attr,
 546	&port_pma_attr_port_rcv_data.attr.attr,
 547	&port_pma_attr_port_xmit_packets.attr.attr,
 548	&port_pma_attr_port_rcv_packets.attr.attr,
 549	NULL
 550};
 551
 552static struct attribute *pma_attrs_ext[] = {
 553	&port_pma_attr_symbol_error.attr.attr,
 554	&port_pma_attr_link_error_recovery.attr.attr,
 555	&port_pma_attr_link_downed.attr.attr,
 556	&port_pma_attr_port_rcv_errors.attr.attr,
 557	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
 558	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
 559	&port_pma_attr_port_xmit_discards.attr.attr,
 560	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
 561	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
 562	&port_pma_attr_local_link_integrity_errors.attr.attr,
 563	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
 564	&port_pma_attr_VL15_dropped.attr.attr,
 565	&port_pma_attr_ext_port_xmit_data.attr.attr,
 566	&port_pma_attr_ext_port_rcv_data.attr.attr,
 567	&port_pma_attr_ext_port_xmit_packets.attr.attr,
 568	&port_pma_attr_ext_port_rcv_packets.attr.attr,
 569	&port_pma_attr_ext_unicast_rcv_packets.attr.attr,
 570	&port_pma_attr_ext_unicast_xmit_packets.attr.attr,
 571	&port_pma_attr_ext_multicast_rcv_packets.attr.attr,
 572	&port_pma_attr_ext_multicast_xmit_packets.attr.attr,
 573	NULL
 574};
 575
 576static struct attribute *pma_attrs_noietf[] = {
 577	&port_pma_attr_symbol_error.attr.attr,
 578	&port_pma_attr_link_error_recovery.attr.attr,
 579	&port_pma_attr_link_downed.attr.attr,
 580	&port_pma_attr_port_rcv_errors.attr.attr,
 581	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
 582	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
 583	&port_pma_attr_port_xmit_discards.attr.attr,
 584	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
 585	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
 586	&port_pma_attr_local_link_integrity_errors.attr.attr,
 587	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
 588	&port_pma_attr_VL15_dropped.attr.attr,
 589	&port_pma_attr_ext_port_xmit_data.attr.attr,
 590	&port_pma_attr_ext_port_rcv_data.attr.attr,
 591	&port_pma_attr_ext_port_xmit_packets.attr.attr,
 592	&port_pma_attr_ext_port_rcv_packets.attr.attr,
 593	NULL
 594};
 595
 596static struct attribute_group pma_group = {
 597	.name  = "counters",
 598	.attrs  = pma_attrs
 599};
 600
 601static struct attribute_group pma_group_ext = {
 602	.name  = "counters",
 603	.attrs  = pma_attrs_ext
 604};
 605
 606static struct attribute_group pma_group_noietf = {
 607	.name  = "counters",
 608	.attrs  = pma_attrs_noietf
 609};
 610
 611static void ib_port_release(struct kobject *kobj)
 612{
 613	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
 614	struct attribute *a;
 615	int i;
 616
 617	if (p->gid_group.attrs) {
 618		for (i = 0; (a = p->gid_group.attrs[i]); ++i)
 619			kfree(a);
 620
 621		kfree(p->gid_group.attrs);
 622	}
 623
 624	if (p->pkey_group.attrs) {
 625		for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
 626			kfree(a);
 627
 628		kfree(p->pkey_group.attrs);
 629	}
 630
 631	kfree(p);
 632}
 633
 634static void ib_port_gid_attr_release(struct kobject *kobj)
 
 
 
 
 
 
 635{
 636	struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
 637						kobj);
 638	struct attribute *a;
 639	int i;
 640
 641	if (g->ndev.attrs) {
 642		for (i = 0; (a = g->ndev.attrs[i]); ++i)
 643			kfree(a);
 644
 645		kfree(g->ndev.attrs);
 646	}
 
 
 647
 648	if (g->type.attrs) {
 649		for (i = 0; (a = g->type.attrs[i]); ++i)
 650			kfree(a);
 651
 652		kfree(g->type.attrs);
 653	}
 
 654
 655	kfree(g);
 656}
 657
 658static struct kobj_type port_type = {
 659	.release       = ib_port_release,
 660	.sysfs_ops     = &port_sysfs_ops,
 661	.default_attrs = port_default_attrs
 662};
 663
 664static struct kobj_type gid_attr_type = {
 665	.sysfs_ops      = &gid_attr_sysfs_ops,
 666	.release        = ib_port_gid_attr_release
 667};
 668
 669static struct attribute **
 670alloc_group_attrs(ssize_t (*show)(struct ib_port *,
 671				  struct port_attribute *, char *buf),
 672		  int len)
 673{
 674	struct attribute **tab_attr;
 675	struct port_table_attribute *element;
 676	int i;
 677
 678	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
 679	if (!tab_attr)
 680		return NULL;
 681
 682	for (i = 0; i < len; i++) {
 683		element = kzalloc(sizeof(struct port_table_attribute),
 684				  GFP_KERNEL);
 685		if (!element)
 686			goto err;
 687
 688		if (snprintf(element->name, sizeof(element->name),
 689			     "%d", i) >= sizeof(element->name)) {
 690			kfree(element);
 691			goto err;
 692		}
 693
 694		element->attr.attr.name  = element->name;
 695		element->attr.attr.mode  = S_IRUGO;
 696		element->attr.show       = show;
 697		element->index		 = i;
 698		sysfs_attr_init(&element->attr.attr);
 699
 700		tab_attr[i] = &element->attr.attr;
 701	}
 702
 703	return tab_attr;
 704
 705err:
 706	while (--i >= 0)
 707		kfree(tab_attr[i]);
 708	kfree(tab_attr);
 709	return NULL;
 710}
 711
 712/*
 713 * Figure out which counter table to use depending on
 714 * the device capabilities.
 715 */
 716static struct attribute_group *get_counter_table(struct ib_device *dev,
 717						 int port_num)
 718{
 719	struct ib_class_port_info cpi;
 720
 721	if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
 722				&cpi, 40, sizeof(cpi)) >= 0) {
 723		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
 724			/* We have extended counters */
 725			return &pma_group_ext;
 726
 727		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
 728			/* But not the IETF ones */
 729			return &pma_group_noietf;
 730	}
 731
 732	/* Fall back to normal counters */
 733	return &pma_group;
 734}
 735
 736static int add_port(struct ib_device *device, int port_num,
 737		    int (*port_callback)(struct ib_device *,
 738					 u8, struct kobject *))
 739{
 740	struct ib_port *p;
 741	struct ib_port_attr attr;
 742	int i;
 743	int ret;
 744
 745	ret = ib_query_port(device, port_num, &attr);
 746	if (ret)
 747		return ret;
 748
 749	p = kzalloc(sizeof *p, GFP_KERNEL);
 750	if (!p)
 751		return -ENOMEM;
 752
 753	p->ibdev      = device;
 754	p->port_num   = port_num;
 755
 756	ret = kobject_init_and_add(&p->kobj, &port_type,
 757				   device->ports_parent,
 758				   "%d", port_num);
 759	if (ret) {
 760		kfree(p);
 761		return ret;
 762	}
 763
 764	p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
 765	if (!p->gid_attr_group) {
 766		ret = -ENOMEM;
 767		goto err_put;
 768	}
 769
 770	p->gid_attr_group->port = p;
 771	ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
 772				   &p->kobj, "gid_attrs");
 773	if (ret) {
 774		kfree(p->gid_attr_group);
 775		goto err_put;
 776	}
 777
 778	p->pma_table = get_counter_table(device, port_num);
 779	ret = sysfs_create_group(&p->kobj, p->pma_table);
 780	if (ret)
 781		goto err_put_gid_attrs;
 782
 783	p->gid_group.name  = "gids";
 784	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
 785	if (!p->gid_group.attrs) {
 786		ret = -ENOMEM;
 787		goto err_remove_pma;
 788	}
 789
 790	ret = sysfs_create_group(&p->kobj, &p->gid_group);
 791	if (ret)
 792		goto err_free_gid;
 793
 794	p->gid_attr_group->ndev.name = "ndevs";
 795	p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
 796							  attr.gid_tbl_len);
 797	if (!p->gid_attr_group->ndev.attrs) {
 798		ret = -ENOMEM;
 799		goto err_remove_gid;
 800	}
 801
 802	ret = sysfs_create_group(&p->gid_attr_group->kobj,
 803				 &p->gid_attr_group->ndev);
 804	if (ret)
 805		goto err_free_gid_ndev;
 806
 807	p->gid_attr_group->type.name = "types";
 808	p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
 809							  attr.gid_tbl_len);
 810	if (!p->gid_attr_group->type.attrs) {
 811		ret = -ENOMEM;
 812		goto err_remove_gid_ndev;
 813	}
 814
 815	ret = sysfs_create_group(&p->gid_attr_group->kobj,
 816				 &p->gid_attr_group->type);
 817	if (ret)
 818		goto err_free_gid_type;
 819
 820	p->pkey_group.name  = "pkeys";
 821	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
 822						attr.pkey_tbl_len);
 823	if (!p->pkey_group.attrs) {
 824		ret = -ENOMEM;
 825		goto err_remove_gid_type;
 826	}
 827
 828	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
 829	if (ret)
 830		goto err_free_pkey;
 831
 832	if (port_callback) {
 833		ret = port_callback(device, port_num, &p->kobj);
 834		if (ret)
 835			goto err_remove_pkey;
 836	}
 837
 838	list_add_tail(&p->kobj.entry, &device->port_list);
 839
 840	kobject_uevent(&p->kobj, KOBJ_ADD);
 841	return 0;
 842
 843err_remove_pkey:
 844	sysfs_remove_group(&p->kobj, &p->pkey_group);
 845
 846err_free_pkey:
 847	for (i = 0; i < attr.pkey_tbl_len; ++i)
 848		kfree(p->pkey_group.attrs[i]);
 849
 850	kfree(p->pkey_group.attrs);
 851	p->pkey_group.attrs = NULL;
 852
 853err_remove_gid_type:
 854	sysfs_remove_group(&p->gid_attr_group->kobj,
 855			   &p->gid_attr_group->type);
 856
 857err_free_gid_type:
 858	for (i = 0; i < attr.gid_tbl_len; ++i)
 859		kfree(p->gid_attr_group->type.attrs[i]);
 860
 861	kfree(p->gid_attr_group->type.attrs);
 862	p->gid_attr_group->type.attrs = NULL;
 863
 864err_remove_gid_ndev:
 865	sysfs_remove_group(&p->gid_attr_group->kobj,
 866			   &p->gid_attr_group->ndev);
 867
 868err_free_gid_ndev:
 869	for (i = 0; i < attr.gid_tbl_len; ++i)
 870		kfree(p->gid_attr_group->ndev.attrs[i]);
 871
 872	kfree(p->gid_attr_group->ndev.attrs);
 873	p->gid_attr_group->ndev.attrs = NULL;
 874
 875err_remove_gid:
 876	sysfs_remove_group(&p->kobj, &p->gid_group);
 877
 878err_free_gid:
 879	for (i = 0; i < attr.gid_tbl_len; ++i)
 880		kfree(p->gid_group.attrs[i]);
 881
 882	kfree(p->gid_group.attrs);
 883	p->gid_group.attrs = NULL;
 884
 885err_remove_pma:
 886	sysfs_remove_group(&p->kobj, p->pma_table);
 887
 888err_put_gid_attrs:
 889	kobject_put(&p->gid_attr_group->kobj);
 890
 891err_put:
 892	kobject_put(&p->kobj);
 
 893	return ret;
 894}
 895
 896static ssize_t show_node_type(struct device *device,
 897			      struct device_attribute *attr, char *buf)
 898{
 899	struct ib_device *dev = container_of(device, struct ib_device, dev);
 900
 901	switch (dev->node_type) {
 902	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
 903	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
 904	case RDMA_NODE_USNIC:	  return sprintf(buf, "%d: usNIC\n", dev->node_type);
 905	case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
 906	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
 907	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
 908	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
 909	}
 910}
 911
 912static ssize_t show_sys_image_guid(struct device *device,
 913				   struct device_attribute *dev_attr, char *buf)
 914{
 915	struct ib_device *dev = container_of(device, struct ib_device, dev);
 
 
 
 
 
 
 916
 917	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
 918		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
 919		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
 920		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
 921		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
 922}
 923
 924static ssize_t show_node_guid(struct device *device,
 925			      struct device_attribute *attr, char *buf)
 926{
 927	struct ib_device *dev = container_of(device, struct ib_device, dev);
 928
 929	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
 930		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
 931		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
 932		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
 933		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
 934}
 935
 936static ssize_t show_node_desc(struct device *device,
 937			      struct device_attribute *attr, char *buf)
 938{
 939	struct ib_device *dev = container_of(device, struct ib_device, dev);
 940
 941	return sprintf(buf, "%.64s\n", dev->node_desc);
 942}
 943
 944static ssize_t set_node_desc(struct device *device,
 945			     struct device_attribute *attr,
 946			     const char *buf, size_t count)
 947{
 948	struct ib_device *dev = container_of(device, struct ib_device, dev);
 949	struct ib_device_modify desc = {};
 950	int ret;
 951
 952	if (!dev->modify_device)
 953		return -EIO;
 954
 955	memcpy(desc.node_desc, buf, min_t(int, count, 64));
 956	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
 957	if (ret)
 958		return ret;
 959
 960	return count;
 961}
 962
 963static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
 964static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
 965static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
 966static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
 967
 968static struct device_attribute *ib_class_attributes[] = {
 969	&dev_attr_node_type,
 970	&dev_attr_sys_image_guid,
 971	&dev_attr_node_guid,
 972	&dev_attr_node_desc
 973};
 974
 
 
 
 
 
 
 975/* Show a given an attribute in the statistics group */
 976static ssize_t show_protocol_stat(const struct device *device,
 977			    struct device_attribute *attr, char *buf,
 978			    unsigned offset)
 979{
 980	struct ib_device *dev = container_of(device, struct ib_device, dev);
 981	union rdma_protocol_stats stats;
 982	ssize_t ret;
 983
 984	ret = dev->get_protocol_stats(dev, &stats);
 985	if (ret)
 986		return ret;
 987
 988	return sprintf(buf, "%llu\n",
 989		       (unsigned long long) ((u64 *) &stats)[offset]);
 990}
 991
 992/* generate a read-only iwarp statistics attribute */
 993#define IW_STATS_ENTRY(name)						\
 994static ssize_t show_##name(struct device *device,			\
 995			   struct device_attribute *attr, char *buf)	\
 996{									\
 997	return show_protocol_stat(device, attr, buf,			\
 998				  offsetof(struct iw_protocol_stats, name) / \
 999				  sizeof (u64));			\
1000}									\
1001static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
1002
1003IW_STATS_ENTRY(ipInReceives);
1004IW_STATS_ENTRY(ipInHdrErrors);
1005IW_STATS_ENTRY(ipInTooBigErrors);
1006IW_STATS_ENTRY(ipInNoRoutes);
1007IW_STATS_ENTRY(ipInAddrErrors);
1008IW_STATS_ENTRY(ipInUnknownProtos);
1009IW_STATS_ENTRY(ipInTruncatedPkts);
1010IW_STATS_ENTRY(ipInDiscards);
1011IW_STATS_ENTRY(ipInDelivers);
1012IW_STATS_ENTRY(ipOutForwDatagrams);
1013IW_STATS_ENTRY(ipOutRequests);
1014IW_STATS_ENTRY(ipOutDiscards);
1015IW_STATS_ENTRY(ipOutNoRoutes);
1016IW_STATS_ENTRY(ipReasmTimeout);
1017IW_STATS_ENTRY(ipReasmReqds);
1018IW_STATS_ENTRY(ipReasmOKs);
1019IW_STATS_ENTRY(ipReasmFails);
1020IW_STATS_ENTRY(ipFragOKs);
1021IW_STATS_ENTRY(ipFragFails);
1022IW_STATS_ENTRY(ipFragCreates);
1023IW_STATS_ENTRY(ipInMcastPkts);
1024IW_STATS_ENTRY(ipOutMcastPkts);
1025IW_STATS_ENTRY(ipInBcastPkts);
1026IW_STATS_ENTRY(ipOutBcastPkts);
1027IW_STATS_ENTRY(tcpRtoAlgorithm);
1028IW_STATS_ENTRY(tcpRtoMin);
1029IW_STATS_ENTRY(tcpRtoMax);
1030IW_STATS_ENTRY(tcpMaxConn);
1031IW_STATS_ENTRY(tcpActiveOpens);
1032IW_STATS_ENTRY(tcpPassiveOpens);
1033IW_STATS_ENTRY(tcpAttemptFails);
1034IW_STATS_ENTRY(tcpEstabResets);
1035IW_STATS_ENTRY(tcpCurrEstab);
1036IW_STATS_ENTRY(tcpInSegs);
1037IW_STATS_ENTRY(tcpOutSegs);
1038IW_STATS_ENTRY(tcpRetransSegs);
1039IW_STATS_ENTRY(tcpInErrs);
1040IW_STATS_ENTRY(tcpOutRsts);
1041
1042static struct attribute *iw_proto_stats_attrs[] = {
1043	&dev_attr_ipInReceives.attr,
1044	&dev_attr_ipInHdrErrors.attr,
1045	&dev_attr_ipInTooBigErrors.attr,
1046	&dev_attr_ipInNoRoutes.attr,
1047	&dev_attr_ipInAddrErrors.attr,
1048	&dev_attr_ipInUnknownProtos.attr,
1049	&dev_attr_ipInTruncatedPkts.attr,
1050	&dev_attr_ipInDiscards.attr,
1051	&dev_attr_ipInDelivers.attr,
1052	&dev_attr_ipOutForwDatagrams.attr,
1053	&dev_attr_ipOutRequests.attr,
1054	&dev_attr_ipOutDiscards.attr,
1055	&dev_attr_ipOutNoRoutes.attr,
1056	&dev_attr_ipReasmTimeout.attr,
1057	&dev_attr_ipReasmReqds.attr,
1058	&dev_attr_ipReasmOKs.attr,
1059	&dev_attr_ipReasmFails.attr,
1060	&dev_attr_ipFragOKs.attr,
1061	&dev_attr_ipFragFails.attr,
1062	&dev_attr_ipFragCreates.attr,
1063	&dev_attr_ipInMcastPkts.attr,
1064	&dev_attr_ipOutMcastPkts.attr,
1065	&dev_attr_ipInBcastPkts.attr,
1066	&dev_attr_ipOutBcastPkts.attr,
1067	&dev_attr_tcpRtoAlgorithm.attr,
1068	&dev_attr_tcpRtoMin.attr,
1069	&dev_attr_tcpRtoMax.attr,
1070	&dev_attr_tcpMaxConn.attr,
1071	&dev_attr_tcpActiveOpens.attr,
1072	&dev_attr_tcpPassiveOpens.attr,
1073	&dev_attr_tcpAttemptFails.attr,
1074	&dev_attr_tcpEstabResets.attr,
1075	&dev_attr_tcpCurrEstab.attr,
1076	&dev_attr_tcpInSegs.attr,
1077	&dev_attr_tcpOutSegs.attr,
1078	&dev_attr_tcpRetransSegs.attr,
1079	&dev_attr_tcpInErrs.attr,
1080	&dev_attr_tcpOutRsts.attr,
1081	NULL
1082};
1083
1084static struct attribute_group iw_stats_group = {
1085	.name	= "proto_stats",
1086	.attrs	= iw_proto_stats_attrs,
1087};
1088
1089static void free_port_list_attributes(struct ib_device *device)
1090{
1091	struct kobject *p, *t;
1092
1093	list_for_each_entry_safe(p, t, &device->port_list, entry) {
1094		struct ib_port *port = container_of(p, struct ib_port, kobj);
1095		list_del(&p->entry);
1096		sysfs_remove_group(p, port->pma_table);
1097		sysfs_remove_group(p, &port->pkey_group);
1098		sysfs_remove_group(p, &port->gid_group);
1099		sysfs_remove_group(&port->gid_attr_group->kobj,
1100				   &port->gid_attr_group->ndev);
1101		sysfs_remove_group(&port->gid_attr_group->kobj,
1102				   &port->gid_attr_group->type);
1103		kobject_put(&port->gid_attr_group->kobj);
1104		kobject_put(p);
1105	}
1106
1107	kobject_put(device->ports_parent);
1108}
1109
1110int ib_device_register_sysfs(struct ib_device *device,
1111			     int (*port_callback)(struct ib_device *,
1112						  u8, struct kobject *))
1113{
1114	struct device *class_dev = &device->dev;
1115	int ret;
1116	int i;
1117
1118	device->dev.parent = device->dma_device;
1119	ret = dev_set_name(class_dev, "%s", device->name);
1120	if (ret)
1121		return ret;
 
 
1122
1123	ret = device_add(class_dev);
1124	if (ret)
1125		goto err;
1126
1127	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
1128		ret = device_create_file(class_dev, ib_class_attributes[i]);
1129		if (ret)
1130			goto err_unregister;
1131	}
1132
1133	device->ports_parent = kobject_create_and_add("ports",
1134						      &class_dev->kobj);
1135	if (!device->ports_parent) {
1136		ret = -ENOMEM;
1137		goto err_put;
1138	}
1139
1140	if (rdma_cap_ib_switch(device)) {
1141		ret = add_port(device, 0, port_callback);
1142		if (ret)
1143			goto err_put;
1144	} else {
1145		for (i = 1; i <= device->phys_port_cnt; ++i) {
1146			ret = add_port(device, i, port_callback);
1147			if (ret)
1148				goto err_put;
1149		}
1150	}
1151
1152	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
1153		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
1154		if (ret)
1155			goto err_put;
1156	}
1157
1158	return 0;
1159
1160err_put:
1161	free_port_list_attributes(device);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1162
1163err_unregister:
1164	device_unregister(class_dev);
1165
1166err:
1167	return ret;
1168}
1169
1170void ib_device_unregister_sysfs(struct ib_device *device)
1171{
 
 
 
1172	/* Hold kobject until ib_dealloc_device() */
1173	struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
1174	int i;
1175
1176	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
1177		sysfs_remove_group(kobj_dev, &iw_stats_group);
 
 
 
 
 
 
1178
1179	free_port_list_attributes(device);
 
 
1180
1181	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
1182		device_remove_file(&device->dev, ib_class_attributes[i]);
 
 
1183
1184	device_unregister(&device->dev);
 
 
1185}