Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2/*
  3 * Copyright(c) 2015-2017 Intel Corporation.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#include <linux/ctype.h>
  7#include <rdma/ib_sysfs.h>
  8
  9#include "hfi.h"
 10#include "mad.h"
 11#include "trace.h"
 12
 13static struct hfi1_pportdata *hfi1_get_pportdata_kobj(struct kobject *kobj)
 14{
 15	u32 port_num;
 16	struct ib_device *ibdev = ib_port_sysfs_get_ibdev_kobj(kobj, &port_num);
 17	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
 18
 19	return &dd->pport[port_num - 1];
 20}
 21
 22/*
 23 * Start of per-port congestion control structures and support code
 24 */
 25
 26/*
 27 * Congestion control table size followed by table entries
 28 */
 29static ssize_t cc_table_bin_read(struct file *filp, struct kobject *kobj,
 30				 struct bin_attribute *bin_attr, char *buf,
 31				 loff_t pos, size_t count)
 32{
 33	int ret;
 34	struct hfi1_pportdata *ppd = hfi1_get_pportdata_kobj(kobj);
 
 35	struct cc_state *cc_state;
 36
 37	ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
 38		 + sizeof(__be16);
 39
 40	if (pos > ret)
 41		return -EINVAL;
 42
 43	if (count > ret - pos)
 44		count = ret - pos;
 45
 46	if (!count)
 47		return count;
 48
 49	rcu_read_lock();
 50	cc_state = get_cc_state(ppd);
 51	if (!cc_state) {
 52		rcu_read_unlock();
 53		return -EINVAL;
 54	}
 55	memcpy(buf, (void *)&cc_state->cct + pos, count);
 56	rcu_read_unlock();
 57
 58	return count;
 59}
 60static BIN_ATTR_RO(cc_table_bin, PAGE_SIZE);
 
 
 
 
 
 
 
 
 
 
 61
 62/*
 63 * Congestion settings: port control, control map and an array of 16
 64 * entries for the congestion entries - increase, timer, event log
 65 * trigger threshold and the minimum injection rate delay.
 66 */
 67static ssize_t cc_setting_bin_read(struct file *filp, struct kobject *kobj,
 68				   struct bin_attribute *bin_attr,
 69				   char *buf, loff_t pos, size_t count)
 70{
 71	struct hfi1_pportdata *ppd = hfi1_get_pportdata_kobj(kobj);
 72	int ret;
 
 
 73	struct cc_state *cc_state;
 74
 75	ret = sizeof(struct opa_congestion_setting_attr_shadow);
 76
 77	if (pos > ret)
 78		return -EINVAL;
 79	if (count > ret - pos)
 80		count = ret - pos;
 81
 82	if (!count)
 83		return count;
 84
 85	rcu_read_lock();
 86	cc_state = get_cc_state(ppd);
 87	if (!cc_state) {
 88		rcu_read_unlock();
 89		return -EINVAL;
 90	}
 91	memcpy(buf, (void *)&cc_state->cong_setting + pos, count);
 92	rcu_read_unlock();
 93
 94	return count;
 95}
 96static BIN_ATTR_RO(cc_setting_bin, PAGE_SIZE);
 97
 98static struct bin_attribute *port_cc_bin_attributes[] = {
 99	&bin_attr_cc_setting_bin,
100	&bin_attr_cc_table_bin,
101	NULL
102};
103
104static ssize_t cc_prescan_show(struct ib_device *ibdev, u32 port_num,
105			       struct ib_port_attribute *attr, char *buf)
106{
107	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
108	struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
109
110	return sysfs_emit(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
 
 
111}
112
113static ssize_t cc_prescan_store(struct ib_device *ibdev, u32 port_num,
114				struct ib_port_attribute *attr, const char *buf,
115				size_t count)
116{
117	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
118	struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
119
120	if (!memcmp(buf, "on", 2))
121		ppd->cc_prescan = true;
122	else if (!memcmp(buf, "off", 3))
123		ppd->cc_prescan = false;
124
125	return count;
126}
127static IB_PORT_ATTR_ADMIN_RW(cc_prescan);
128
129static struct attribute *port_cc_attributes[] = {
130	&ib_port_attr_cc_prescan.attr,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131	NULL
132};
133
134static const struct attribute_group port_cc_group = {
135	.name = "CCMgtA",
136	.attrs = port_cc_attributes,
137	.bin_attrs = port_cc_bin_attributes,
138};
139
140/* Start sc2vl */
 
 
 
 
 
 
141struct hfi1_sc2vl_attr {
142	struct ib_port_attribute attr;
143	int sc;
144};
145
146static ssize_t sc2vl_attr_show(struct ib_device *ibdev, u32 port_num,
147			       struct ib_port_attribute *attr, char *buf)
148{
149	struct hfi1_sc2vl_attr *sattr =
150		container_of(attr, struct hfi1_sc2vl_attr, attr);
151	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
152
153	return sysfs_emit(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
154}
155
156#define HFI1_SC2VL_ATTR(N)                                                     \
157	static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = {                  \
158		.attr = __ATTR(N, 0444, sc2vl_attr_show, NULL),                \
159		.sc = N,                                                       \
160	}
161
162HFI1_SC2VL_ATTR(0);
163HFI1_SC2VL_ATTR(1);
164HFI1_SC2VL_ATTR(2);
165HFI1_SC2VL_ATTR(3);
166HFI1_SC2VL_ATTR(4);
167HFI1_SC2VL_ATTR(5);
168HFI1_SC2VL_ATTR(6);
169HFI1_SC2VL_ATTR(7);
170HFI1_SC2VL_ATTR(8);
171HFI1_SC2VL_ATTR(9);
172HFI1_SC2VL_ATTR(10);
173HFI1_SC2VL_ATTR(11);
174HFI1_SC2VL_ATTR(12);
175HFI1_SC2VL_ATTR(13);
176HFI1_SC2VL_ATTR(14);
177HFI1_SC2VL_ATTR(15);
178HFI1_SC2VL_ATTR(16);
179HFI1_SC2VL_ATTR(17);
180HFI1_SC2VL_ATTR(18);
181HFI1_SC2VL_ATTR(19);
182HFI1_SC2VL_ATTR(20);
183HFI1_SC2VL_ATTR(21);
184HFI1_SC2VL_ATTR(22);
185HFI1_SC2VL_ATTR(23);
186HFI1_SC2VL_ATTR(24);
187HFI1_SC2VL_ATTR(25);
188HFI1_SC2VL_ATTR(26);
189HFI1_SC2VL_ATTR(27);
190HFI1_SC2VL_ATTR(28);
191HFI1_SC2VL_ATTR(29);
192HFI1_SC2VL_ATTR(30);
193HFI1_SC2VL_ATTR(31);
194
195static struct attribute *port_sc2vl_attributes[] = {
196	&hfi1_sc2vl_attr_0.attr.attr,
197	&hfi1_sc2vl_attr_1.attr.attr,
198	&hfi1_sc2vl_attr_2.attr.attr,
199	&hfi1_sc2vl_attr_3.attr.attr,
200	&hfi1_sc2vl_attr_4.attr.attr,
201	&hfi1_sc2vl_attr_5.attr.attr,
202	&hfi1_sc2vl_attr_6.attr.attr,
203	&hfi1_sc2vl_attr_7.attr.attr,
204	&hfi1_sc2vl_attr_8.attr.attr,
205	&hfi1_sc2vl_attr_9.attr.attr,
206	&hfi1_sc2vl_attr_10.attr.attr,
207	&hfi1_sc2vl_attr_11.attr.attr,
208	&hfi1_sc2vl_attr_12.attr.attr,
209	&hfi1_sc2vl_attr_13.attr.attr,
210	&hfi1_sc2vl_attr_14.attr.attr,
211	&hfi1_sc2vl_attr_15.attr.attr,
212	&hfi1_sc2vl_attr_16.attr.attr,
213	&hfi1_sc2vl_attr_17.attr.attr,
214	&hfi1_sc2vl_attr_18.attr.attr,
215	&hfi1_sc2vl_attr_19.attr.attr,
216	&hfi1_sc2vl_attr_20.attr.attr,
217	&hfi1_sc2vl_attr_21.attr.attr,
218	&hfi1_sc2vl_attr_22.attr.attr,
219	&hfi1_sc2vl_attr_23.attr.attr,
220	&hfi1_sc2vl_attr_24.attr.attr,
221	&hfi1_sc2vl_attr_25.attr.attr,
222	&hfi1_sc2vl_attr_26.attr.attr,
223	&hfi1_sc2vl_attr_27.attr.attr,
224	&hfi1_sc2vl_attr_28.attr.attr,
225	&hfi1_sc2vl_attr_29.attr.attr,
226	&hfi1_sc2vl_attr_30.attr.attr,
227	&hfi1_sc2vl_attr_31.attr.attr,
228	NULL
229};
230
231static const struct attribute_group port_sc2vl_group = {
232	.name = "sc2vl",
233	.attrs = port_sc2vl_attributes,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234};
 
235/* End sc2vl */
236
237/* Start sl2sc */
 
 
 
 
 
 
238struct hfi1_sl2sc_attr {
239	struct ib_port_attribute attr;
240	int sl;
241};
242
243static ssize_t sl2sc_attr_show(struct ib_device *ibdev, u32 port_num,
244			       struct ib_port_attribute *attr, char *buf)
245{
246	struct hfi1_sl2sc_attr *sattr =
247		container_of(attr, struct hfi1_sl2sc_attr, attr);
248	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
249	struct hfi1_ibport *ibp = &dd->pport[port_num - 1].ibport_data;
250
251	return sysfs_emit(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
252}
253
254#define HFI1_SL2SC_ATTR(N)                                                     \
255	static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = {                  \
256		.attr = __ATTR(N, 0444, sl2sc_attr_show, NULL), .sl = N        \
257	}
258
259HFI1_SL2SC_ATTR(0);
260HFI1_SL2SC_ATTR(1);
261HFI1_SL2SC_ATTR(2);
262HFI1_SL2SC_ATTR(3);
263HFI1_SL2SC_ATTR(4);
264HFI1_SL2SC_ATTR(5);
265HFI1_SL2SC_ATTR(6);
266HFI1_SL2SC_ATTR(7);
267HFI1_SL2SC_ATTR(8);
268HFI1_SL2SC_ATTR(9);
269HFI1_SL2SC_ATTR(10);
270HFI1_SL2SC_ATTR(11);
271HFI1_SL2SC_ATTR(12);
272HFI1_SL2SC_ATTR(13);
273HFI1_SL2SC_ATTR(14);
274HFI1_SL2SC_ATTR(15);
275HFI1_SL2SC_ATTR(16);
276HFI1_SL2SC_ATTR(17);
277HFI1_SL2SC_ATTR(18);
278HFI1_SL2SC_ATTR(19);
279HFI1_SL2SC_ATTR(20);
280HFI1_SL2SC_ATTR(21);
281HFI1_SL2SC_ATTR(22);
282HFI1_SL2SC_ATTR(23);
283HFI1_SL2SC_ATTR(24);
284HFI1_SL2SC_ATTR(25);
285HFI1_SL2SC_ATTR(26);
286HFI1_SL2SC_ATTR(27);
287HFI1_SL2SC_ATTR(28);
288HFI1_SL2SC_ATTR(29);
289HFI1_SL2SC_ATTR(30);
290HFI1_SL2SC_ATTR(31);
291
292static struct attribute *port_sl2sc_attributes[] = {
293	&hfi1_sl2sc_attr_0.attr.attr,
294	&hfi1_sl2sc_attr_1.attr.attr,
295	&hfi1_sl2sc_attr_2.attr.attr,
296	&hfi1_sl2sc_attr_3.attr.attr,
297	&hfi1_sl2sc_attr_4.attr.attr,
298	&hfi1_sl2sc_attr_5.attr.attr,
299	&hfi1_sl2sc_attr_6.attr.attr,
300	&hfi1_sl2sc_attr_7.attr.attr,
301	&hfi1_sl2sc_attr_8.attr.attr,
302	&hfi1_sl2sc_attr_9.attr.attr,
303	&hfi1_sl2sc_attr_10.attr.attr,
304	&hfi1_sl2sc_attr_11.attr.attr,
305	&hfi1_sl2sc_attr_12.attr.attr,
306	&hfi1_sl2sc_attr_13.attr.attr,
307	&hfi1_sl2sc_attr_14.attr.attr,
308	&hfi1_sl2sc_attr_15.attr.attr,
309	&hfi1_sl2sc_attr_16.attr.attr,
310	&hfi1_sl2sc_attr_17.attr.attr,
311	&hfi1_sl2sc_attr_18.attr.attr,
312	&hfi1_sl2sc_attr_19.attr.attr,
313	&hfi1_sl2sc_attr_20.attr.attr,
314	&hfi1_sl2sc_attr_21.attr.attr,
315	&hfi1_sl2sc_attr_22.attr.attr,
316	&hfi1_sl2sc_attr_23.attr.attr,
317	&hfi1_sl2sc_attr_24.attr.attr,
318	&hfi1_sl2sc_attr_25.attr.attr,
319	&hfi1_sl2sc_attr_26.attr.attr,
320	&hfi1_sl2sc_attr_27.attr.attr,
321	&hfi1_sl2sc_attr_28.attr.attr,
322	&hfi1_sl2sc_attr_29.attr.attr,
323	&hfi1_sl2sc_attr_30.attr.attr,
324	&hfi1_sl2sc_attr_31.attr.attr,
325	NULL
326};
327
328static const struct attribute_group port_sl2sc_group = {
329	.name = "sl2sc",
330	.attrs = port_sl2sc_attributes,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331};
332
333/* End sl2sc */
334
335/* Start vl2mtu */
336
 
 
 
 
 
 
337struct hfi1_vl2mtu_attr {
338	struct ib_port_attribute attr;
339	int vl;
340};
341
342static ssize_t vl2mtu_attr_show(struct ib_device *ibdev, u32 port_num,
343				struct ib_port_attribute *attr, char *buf)
344{
345	struct hfi1_vl2mtu_attr *vlattr =
346		container_of(attr, struct hfi1_vl2mtu_attr, attr);
347	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
348
349	return sysfs_emit(buf, "%u\n", dd->vld[vlattr->vl].mtu);
350}
351
352#define HFI1_VL2MTU_ATTR(N)                                                    \
353	static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = {                \
354		.attr = __ATTR(N, 0444, vl2mtu_attr_show, NULL),               \
355		.vl = N,                                                       \
356	}
357
358HFI1_VL2MTU_ATTR(0);
359HFI1_VL2MTU_ATTR(1);
360HFI1_VL2MTU_ATTR(2);
361HFI1_VL2MTU_ATTR(3);
362HFI1_VL2MTU_ATTR(4);
363HFI1_VL2MTU_ATTR(5);
364HFI1_VL2MTU_ATTR(6);
365HFI1_VL2MTU_ATTR(7);
366HFI1_VL2MTU_ATTR(8);
367HFI1_VL2MTU_ATTR(9);
368HFI1_VL2MTU_ATTR(10);
369HFI1_VL2MTU_ATTR(11);
370HFI1_VL2MTU_ATTR(12);
371HFI1_VL2MTU_ATTR(13);
372HFI1_VL2MTU_ATTR(14);
373HFI1_VL2MTU_ATTR(15);
374
375static struct attribute *port_vl2mtu_attributes[] = {
376	&hfi1_vl2mtu_attr_0.attr.attr,
377	&hfi1_vl2mtu_attr_1.attr.attr,
378	&hfi1_vl2mtu_attr_2.attr.attr,
379	&hfi1_vl2mtu_attr_3.attr.attr,
380	&hfi1_vl2mtu_attr_4.attr.attr,
381	&hfi1_vl2mtu_attr_5.attr.attr,
382	&hfi1_vl2mtu_attr_6.attr.attr,
383	&hfi1_vl2mtu_attr_7.attr.attr,
384	&hfi1_vl2mtu_attr_8.attr.attr,
385	&hfi1_vl2mtu_attr_9.attr.attr,
386	&hfi1_vl2mtu_attr_10.attr.attr,
387	&hfi1_vl2mtu_attr_11.attr.attr,
388	&hfi1_vl2mtu_attr_12.attr.attr,
389	&hfi1_vl2mtu_attr_13.attr.attr,
390	&hfi1_vl2mtu_attr_14.attr.attr,
391	&hfi1_vl2mtu_attr_15.attr.attr,
392	NULL
393};
394
395static const struct attribute_group port_vl2mtu_group = {
396	.name = "vl2mtu",
397	.attrs = port_vl2mtu_attributes,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398};
399
400/* end of per-port file structures and support code */
401
402/*
403 * Start of per-unit (or driver, in some cases, but replicated
404 * per unit) functions (these get a device *)
405 */
406static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
407			   char *buf)
408{
409	struct hfi1_ibdev *dev =
410		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
411
412	return sysfs_emit(buf, "%x\n", dd_from_dev(dev)->minrev);
413}
414static DEVICE_ATTR_RO(hw_rev);
415
416static ssize_t board_id_show(struct device *device,
417			     struct device_attribute *attr, char *buf)
418{
419	struct hfi1_ibdev *dev =
420		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
421	struct hfi1_devdata *dd = dd_from_dev(dev);
 
422
423	if (!dd->boardname)
424		return -EINVAL;
425
426	return sysfs_emit(buf, "%s\n", dd->boardname);
 
427}
428static DEVICE_ATTR_RO(board_id);
429
430static ssize_t boardversion_show(struct device *device,
431				 struct device_attribute *attr, char *buf)
432{
433	struct hfi1_ibdev *dev =
434		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
435	struct hfi1_devdata *dd = dd_from_dev(dev);
436
437	/* The string printed here is already newline-terminated. */
438	return sysfs_emit(buf, "%s", dd->boardversion);
439}
440static DEVICE_ATTR_RO(boardversion);
441
442static ssize_t nctxts_show(struct device *device,
443			   struct device_attribute *attr, char *buf)
444{
445	struct hfi1_ibdev *dev =
446		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
447	struct hfi1_devdata *dd = dd_from_dev(dev);
448
449	/*
450	 * Return the smaller of send and receive contexts.
451	 * Normally, user level applications would require both a send
452	 * and a receive context, so returning the smaller of the two counts
453	 * give a more accurate picture of total contexts available.
454	 */
455	return sysfs_emit(buf, "%u\n",
456			  min(dd->num_user_contexts,
457			      (u32)dd->sc_sizes[SC_USER].count));
458}
459static DEVICE_ATTR_RO(nctxts);
460
461static ssize_t nfreectxts_show(struct device *device,
462			       struct device_attribute *attr, char *buf)
463{
464	struct hfi1_ibdev *dev =
465		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
466	struct hfi1_devdata *dd = dd_from_dev(dev);
467
468	/* Return the number of free user ports (contexts) available. */
469	return sysfs_emit(buf, "%u\n", dd->freectxts);
470}
471static DEVICE_ATTR_RO(nfreectxts);
472
473static ssize_t serial_show(struct device *device,
474			   struct device_attribute *attr, char *buf)
475{
476	struct hfi1_ibdev *dev =
477		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
478	struct hfi1_devdata *dd = dd_from_dev(dev);
479
480	/* dd->serial is already newline terminated in chip.c */
481	return sysfs_emit(buf, "%s", dd->serial);
482}
483static DEVICE_ATTR_RO(serial);
484
485static ssize_t chip_reset_store(struct device *device,
486				struct device_attribute *attr, const char *buf,
487				size_t count)
488{
489	struct hfi1_ibdev *dev =
490		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
491	struct hfi1_devdata *dd = dd_from_dev(dev);
492	int ret;
493
494	if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
495		ret = -EINVAL;
496		goto bail;
497	}
498
499	ret = hfi1_reset_device(dd->unit);
500bail:
501	return ret < 0 ? ret : count;
502}
503static DEVICE_ATTR_WO(chip_reset);
504
505/*
506 * Convert the reported temperature from an integer (reported in
507 * units of 0.25C) to a floating point number.
508 */
509#define temp_d(t) ((t) >> 2)
510#define temp_f(t) (((t)&0x3) * 25u)
 
511
512/*
513 * Dump tempsense values, in decimal, to ease shell-scripts.
514 */
515static ssize_t tempsense_show(struct device *device,
516			      struct device_attribute *attr, char *buf)
517{
518	struct hfi1_ibdev *dev =
519		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
520	struct hfi1_devdata *dd = dd_from_dev(dev);
521	struct hfi1_temp temp;
522	int ret;
523
524	ret = hfi1_tempsense_rd(dd, &temp);
525	if (ret)
526		return ret;
527
528	return sysfs_emit(buf, "%u.%02u %u.%02u %u.%02u %u.%02u %u %u %u\n",
529			  temp_d(temp.curr), temp_f(temp.curr),
530			  temp_d(temp.lo_lim), temp_f(temp.lo_lim),
531			  temp_d(temp.hi_lim), temp_f(temp.hi_lim),
532			  temp_d(temp.crit_lim), temp_f(temp.crit_lim),
533			  temp.triggers & 0x1,
534			  temp.triggers & 0x2,
535			  temp.triggers & 0x4);
 
 
536}
537static DEVICE_ATTR_RO(tempsense);
538
539/*
540 * end of per-unit (or driver, in some cases, but replicated
541 * per unit) functions
542 */
543
544/* start of per-unit file structures and support code */
545static struct attribute *hfi1_attributes[] = {
546	&dev_attr_hw_rev.attr,
547	&dev_attr_board_id.attr,
548	&dev_attr_nctxts.attr,
549	&dev_attr_nfreectxts.attr,
550	&dev_attr_serial.attr,
551	&dev_attr_boardversion.attr,
552	&dev_attr_tempsense.attr,
553	&dev_attr_chip_reset.attr,
554	NULL,
555};
556
557const struct attribute_group ib_hfi1_attr_group = {
558	.attrs = hfi1_attributes,
559};
560
561const struct attribute_group *hfi1_attr_port_groups[] = {
562	&port_cc_group,
563	&port_sc2vl_group,
564	&port_sl2sc_group,
565	&port_vl2mtu_group,
566	NULL,
567};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
568
569struct sde_attribute {
570	struct attribute attr;
571	ssize_t (*show)(struct sdma_engine *sde, char *buf);
572	ssize_t (*store)(struct sdma_engine *sde, const char *buf, size_t cnt);
573};
574
575static ssize_t sde_show(struct kobject *kobj, struct attribute *attr, char *buf)
576{
577	struct sde_attribute *sde_attr =
578		container_of(attr, struct sde_attribute, attr);
579	struct sdma_engine *sde =
580		container_of(kobj, struct sdma_engine, kobj);
581
582	if (!sde_attr->show)
583		return -EINVAL;
584
585	return sde_attr->show(sde, buf);
586}
587
588static ssize_t sde_store(struct kobject *kobj, struct attribute *attr,
589			 const char *buf, size_t count)
590{
591	struct sde_attribute *sde_attr =
592		container_of(attr, struct sde_attribute, attr);
593	struct sdma_engine *sde =
594		container_of(kobj, struct sdma_engine, kobj);
595
596	if (!capable(CAP_SYS_ADMIN))
597		return -EPERM;
598
599	if (!sde_attr->store)
600		return -EINVAL;
601
602	return sde_attr->store(sde, buf, count);
603}
604
605static const struct sysfs_ops sde_sysfs_ops = {
606	.show = sde_show,
607	.store = sde_store,
608};
609
610static struct kobj_type sde_ktype = {
611	.sysfs_ops = &sde_sysfs_ops,
612};
613
614#define SDE_ATTR(_name, _mode, _show, _store) \
615	struct sde_attribute sde_attr_##_name = \
616		__ATTR(_name, _mode, _show, _store)
617
618static ssize_t sde_show_cpu_to_sde_map(struct sdma_engine *sde, char *buf)
619{
620	return sdma_get_cpu_to_sde_map(sde, buf);
621}
622
623static ssize_t sde_store_cpu_to_sde_map(struct sdma_engine *sde,
624					const char *buf, size_t count)
625{
626	return sdma_set_cpu_to_sde_map(sde, buf, count);
627}
628
629static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
630{
631	int vl;
632
633	vl = sdma_engine_get_vl(sde);
634	if (vl < 0)
635		return vl;
636
637	return sysfs_emit(buf, "%d\n", vl);
638}
639
640static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
641		sde_show_cpu_to_sde_map,
642		sde_store_cpu_to_sde_map);
643static SDE_ATTR(vl, S_IRUGO, sde_show_vl, NULL);
644
645static struct sde_attribute *sde_attribs[] = {
646	&sde_attr_cpu_list,
647	&sde_attr_vl
648};
649
650/*
651 * Register and create our files in /sys/class/infiniband.
652 */
653int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd)
654{
655	struct ib_device *dev = &dd->verbs_dev.rdi.ibdev;
656	struct device *class_dev = &dev->dev;
657	int i, j, ret;
658
659	for (i = 0; i < dd->num_sdma; i++) {
660		ret = kobject_init_and_add(&dd->per_sdma[i].kobj,
661					   &sde_ktype, &class_dev->kobj,
662					   "sdma%d", i);
663		if (ret)
664			goto bail;
665
666		for (j = 0; j < ARRAY_SIZE(sde_attribs); j++) {
667			ret = sysfs_create_file(&dd->per_sdma[i].kobj,
668						&sde_attribs[j]->attr);
669			if (ret)
670				goto bail;
671		}
672	}
673
674	return 0;
675bail:
676	/*
677	 * The function kobject_put() will call kobject_del() if the kobject
678	 * has been added successfully. The sysfs files created under the
679	 * kobject directory will also be removed during the process.
680	 */
681	for (; i >= 0; i--)
682		kobject_put(&dd->per_sdma[i].kobj);
683
684	return ret;
685}
686
687/*
688 * Unregister and remove our files in /sys/class/infiniband.
689 */
690void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd)
691{
 
692	int i;
693
694	/* Unwind operations in hfi1_verbs_register_sysfs() */
695	for (i = 0; i < dd->num_sdma; i++)
696		kobject_put(&dd->per_sdma[i].kobj);
 
 
 
 
 
 
 
 
 
 
 
 
 
697}
v5.9
 
  1/*
  2 * Copyright(c) 2015-2017 Intel Corporation.
  3 *
  4 * This file is provided under a dual BSD/GPLv2 license.  When using or
  5 * redistributing this file, you may do so under either license.
  6 *
  7 * GPL LICENSE SUMMARY
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of version 2 of the GNU General Public License as
 11 * published by the Free Software Foundation.
 12 *
 13 * This program is distributed in the hope that it will be useful, but
 14 * WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * General Public License for more details.
 17 *
 18 * BSD LICENSE
 19 *
 20 * Redistribution and use in source and binary forms, with or without
 21 * modification, are permitted provided that the following conditions
 22 * are met:
 23 *
 24 *  - Redistributions of source code must retain the above copyright
 25 *    notice, this list of conditions and the following disclaimer.
 26 *  - Redistributions in binary form must reproduce the above copyright
 27 *    notice, this list of conditions and the following disclaimer in
 28 *    the documentation and/or other materials provided with the
 29 *    distribution.
 30 *  - Neither the name of Intel Corporation nor the names of its
 31 *    contributors may be used to endorse or promote products derived
 32 *    from this software without specific prior written permission.
 33 *
 34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 45 *
 46 */
 
 47#include <linux/ctype.h>
 
 48
 49#include "hfi.h"
 50#include "mad.h"
 51#include "trace.h"
 52
 
 
 
 
 
 
 
 
 
 53/*
 54 * Start of per-port congestion control structures and support code
 55 */
 56
 57/*
 58 * Congestion control table size followed by table entries
 59 */
 60static ssize_t read_cc_table_bin(struct file *filp, struct kobject *kobj,
 61				 struct bin_attribute *bin_attr,
 62				 char *buf, loff_t pos, size_t count)
 63{
 64	int ret;
 65	struct hfi1_pportdata *ppd =
 66		container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
 67	struct cc_state *cc_state;
 68
 69	ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
 70		 + sizeof(__be16);
 71
 72	if (pos > ret)
 73		return -EINVAL;
 74
 75	if (count > ret - pos)
 76		count = ret - pos;
 77
 78	if (!count)
 79		return count;
 80
 81	rcu_read_lock();
 82	cc_state = get_cc_state(ppd);
 83	if (!cc_state) {
 84		rcu_read_unlock();
 85		return -EINVAL;
 86	}
 87	memcpy(buf, (void *)&cc_state->cct + pos, count);
 88	rcu_read_unlock();
 89
 90	return count;
 91}
 92
 93static void port_release(struct kobject *kobj)
 94{
 95	/* nothing to do since memory is freed by hfi1_free_devdata() */
 96}
 97
 98static const struct bin_attribute cc_table_bin_attr = {
 99	.attr = {.name = "cc_table_bin", .mode = 0444},
100	.read = read_cc_table_bin,
101	.size = PAGE_SIZE,
102};
103
104/*
105 * Congestion settings: port control, control map and an array of 16
106 * entries for the congestion entries - increase, timer, event log
107 * trigger threshold and the minimum injection rate delay.
108 */
109static ssize_t read_cc_setting_bin(struct file *filp, struct kobject *kobj,
110				   struct bin_attribute *bin_attr,
111				   char *buf, loff_t pos, size_t count)
112{
 
113	int ret;
114	struct hfi1_pportdata *ppd =
115		container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
116	struct cc_state *cc_state;
117
118	ret = sizeof(struct opa_congestion_setting_attr_shadow);
119
120	if (pos > ret)
121		return -EINVAL;
122	if (count > ret - pos)
123		count = ret - pos;
124
125	if (!count)
126		return count;
127
128	rcu_read_lock();
129	cc_state = get_cc_state(ppd);
130	if (!cc_state) {
131		rcu_read_unlock();
132		return -EINVAL;
133	}
134	memcpy(buf, (void *)&cc_state->cong_setting + pos, count);
135	rcu_read_unlock();
136
137	return count;
138}
 
139
140static const struct bin_attribute cc_setting_bin_attr = {
141	.attr = {.name = "cc_settings_bin", .mode = 0444},
142	.read = read_cc_setting_bin,
143	.size = PAGE_SIZE,
144};
145
146struct hfi1_port_attr {
147	struct attribute attr;
148	ssize_t	(*show)(struct hfi1_pportdata *, char *);
149	ssize_t	(*store)(struct hfi1_pportdata *, const char *, size_t);
150};
151
152static ssize_t cc_prescan_show(struct hfi1_pportdata *ppd, char *buf)
153{
154	return sprintf(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
155}
156
157static ssize_t cc_prescan_store(struct hfi1_pportdata *ppd, const char *buf,
 
158				size_t count)
159{
 
 
 
160	if (!memcmp(buf, "on", 2))
161		ppd->cc_prescan = true;
162	else if (!memcmp(buf, "off", 3))
163		ppd->cc_prescan = false;
164
165	return count;
166}
 
167
168static struct hfi1_port_attr cc_prescan_attr =
169		__ATTR(cc_prescan, 0600, cc_prescan_show, cc_prescan_store);
170
171static ssize_t cc_attr_show(struct kobject *kobj, struct attribute *attr,
172			    char *buf)
173{
174	struct hfi1_port_attr *port_attr =
175		container_of(attr, struct hfi1_port_attr, attr);
176	struct hfi1_pportdata *ppd =
177		container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
178
179	return port_attr->show(ppd, buf);
180}
181
182static ssize_t cc_attr_store(struct kobject *kobj, struct attribute *attr,
183			     const char *buf, size_t count)
184{
185	struct hfi1_port_attr *port_attr =
186		container_of(attr, struct hfi1_port_attr, attr);
187	struct hfi1_pportdata *ppd =
188		container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
189
190	return port_attr->store(ppd, buf, count);
191}
192
193static const struct sysfs_ops port_cc_sysfs_ops = {
194	.show = cc_attr_show,
195	.store = cc_attr_store
196};
197
198static struct attribute *port_cc_default_attributes[] = {
199	&cc_prescan_attr.attr,
200	NULL
201};
202
203static struct kobj_type port_cc_ktype = {
204	.release = port_release,
205	.sysfs_ops = &port_cc_sysfs_ops,
206	.default_attrs = port_cc_default_attributes
207};
208
209/* Start sc2vl */
210#define HFI1_SC2VL_ATTR(N)				    \
211	static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = { \
212		.attr = { .name = __stringify(N), .mode = 0444 }, \
213		.sc = N \
214	}
215
216struct hfi1_sc2vl_attr {
217	struct attribute attr;
218	int sc;
219};
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221HFI1_SC2VL_ATTR(0);
222HFI1_SC2VL_ATTR(1);
223HFI1_SC2VL_ATTR(2);
224HFI1_SC2VL_ATTR(3);
225HFI1_SC2VL_ATTR(4);
226HFI1_SC2VL_ATTR(5);
227HFI1_SC2VL_ATTR(6);
228HFI1_SC2VL_ATTR(7);
229HFI1_SC2VL_ATTR(8);
230HFI1_SC2VL_ATTR(9);
231HFI1_SC2VL_ATTR(10);
232HFI1_SC2VL_ATTR(11);
233HFI1_SC2VL_ATTR(12);
234HFI1_SC2VL_ATTR(13);
235HFI1_SC2VL_ATTR(14);
236HFI1_SC2VL_ATTR(15);
237HFI1_SC2VL_ATTR(16);
238HFI1_SC2VL_ATTR(17);
239HFI1_SC2VL_ATTR(18);
240HFI1_SC2VL_ATTR(19);
241HFI1_SC2VL_ATTR(20);
242HFI1_SC2VL_ATTR(21);
243HFI1_SC2VL_ATTR(22);
244HFI1_SC2VL_ATTR(23);
245HFI1_SC2VL_ATTR(24);
246HFI1_SC2VL_ATTR(25);
247HFI1_SC2VL_ATTR(26);
248HFI1_SC2VL_ATTR(27);
249HFI1_SC2VL_ATTR(28);
250HFI1_SC2VL_ATTR(29);
251HFI1_SC2VL_ATTR(30);
252HFI1_SC2VL_ATTR(31);
253
254static struct attribute *sc2vl_default_attributes[] = {
255	&hfi1_sc2vl_attr_0.attr,
256	&hfi1_sc2vl_attr_1.attr,
257	&hfi1_sc2vl_attr_2.attr,
258	&hfi1_sc2vl_attr_3.attr,
259	&hfi1_sc2vl_attr_4.attr,
260	&hfi1_sc2vl_attr_5.attr,
261	&hfi1_sc2vl_attr_6.attr,
262	&hfi1_sc2vl_attr_7.attr,
263	&hfi1_sc2vl_attr_8.attr,
264	&hfi1_sc2vl_attr_9.attr,
265	&hfi1_sc2vl_attr_10.attr,
266	&hfi1_sc2vl_attr_11.attr,
267	&hfi1_sc2vl_attr_12.attr,
268	&hfi1_sc2vl_attr_13.attr,
269	&hfi1_sc2vl_attr_14.attr,
270	&hfi1_sc2vl_attr_15.attr,
271	&hfi1_sc2vl_attr_16.attr,
272	&hfi1_sc2vl_attr_17.attr,
273	&hfi1_sc2vl_attr_18.attr,
274	&hfi1_sc2vl_attr_19.attr,
275	&hfi1_sc2vl_attr_20.attr,
276	&hfi1_sc2vl_attr_21.attr,
277	&hfi1_sc2vl_attr_22.attr,
278	&hfi1_sc2vl_attr_23.attr,
279	&hfi1_sc2vl_attr_24.attr,
280	&hfi1_sc2vl_attr_25.attr,
281	&hfi1_sc2vl_attr_26.attr,
282	&hfi1_sc2vl_attr_27.attr,
283	&hfi1_sc2vl_attr_28.attr,
284	&hfi1_sc2vl_attr_29.attr,
285	&hfi1_sc2vl_attr_30.attr,
286	&hfi1_sc2vl_attr_31.attr,
287	NULL
288};
289
290static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
291			       char *buf)
292{
293	struct hfi1_sc2vl_attr *sattr =
294		container_of(attr, struct hfi1_sc2vl_attr, attr);
295	struct hfi1_pportdata *ppd =
296		container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
297	struct hfi1_devdata *dd = ppd->dd;
298
299	return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
300}
301
302static const struct sysfs_ops hfi1_sc2vl_ops = {
303	.show = sc2vl_attr_show,
304};
305
306static struct kobj_type hfi1_sc2vl_ktype = {
307	.release = port_release,
308	.sysfs_ops = &hfi1_sc2vl_ops,
309	.default_attrs = sc2vl_default_attributes
310};
311
312/* End sc2vl */
313
314/* Start sl2sc */
315#define HFI1_SL2SC_ATTR(N)				    \
316	static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = {	  \
317		.attr = { .name = __stringify(N), .mode = 0444 }, \
318		.sl = N						  \
319	}
320
321struct hfi1_sl2sc_attr {
322	struct attribute attr;
323	int sl;
324};
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326HFI1_SL2SC_ATTR(0);
327HFI1_SL2SC_ATTR(1);
328HFI1_SL2SC_ATTR(2);
329HFI1_SL2SC_ATTR(3);
330HFI1_SL2SC_ATTR(4);
331HFI1_SL2SC_ATTR(5);
332HFI1_SL2SC_ATTR(6);
333HFI1_SL2SC_ATTR(7);
334HFI1_SL2SC_ATTR(8);
335HFI1_SL2SC_ATTR(9);
336HFI1_SL2SC_ATTR(10);
337HFI1_SL2SC_ATTR(11);
338HFI1_SL2SC_ATTR(12);
339HFI1_SL2SC_ATTR(13);
340HFI1_SL2SC_ATTR(14);
341HFI1_SL2SC_ATTR(15);
342HFI1_SL2SC_ATTR(16);
343HFI1_SL2SC_ATTR(17);
344HFI1_SL2SC_ATTR(18);
345HFI1_SL2SC_ATTR(19);
346HFI1_SL2SC_ATTR(20);
347HFI1_SL2SC_ATTR(21);
348HFI1_SL2SC_ATTR(22);
349HFI1_SL2SC_ATTR(23);
350HFI1_SL2SC_ATTR(24);
351HFI1_SL2SC_ATTR(25);
352HFI1_SL2SC_ATTR(26);
353HFI1_SL2SC_ATTR(27);
354HFI1_SL2SC_ATTR(28);
355HFI1_SL2SC_ATTR(29);
356HFI1_SL2SC_ATTR(30);
357HFI1_SL2SC_ATTR(31);
358
359static struct attribute *sl2sc_default_attributes[] = {
360	&hfi1_sl2sc_attr_0.attr,
361	&hfi1_sl2sc_attr_1.attr,
362	&hfi1_sl2sc_attr_2.attr,
363	&hfi1_sl2sc_attr_3.attr,
364	&hfi1_sl2sc_attr_4.attr,
365	&hfi1_sl2sc_attr_5.attr,
366	&hfi1_sl2sc_attr_6.attr,
367	&hfi1_sl2sc_attr_7.attr,
368	&hfi1_sl2sc_attr_8.attr,
369	&hfi1_sl2sc_attr_9.attr,
370	&hfi1_sl2sc_attr_10.attr,
371	&hfi1_sl2sc_attr_11.attr,
372	&hfi1_sl2sc_attr_12.attr,
373	&hfi1_sl2sc_attr_13.attr,
374	&hfi1_sl2sc_attr_14.attr,
375	&hfi1_sl2sc_attr_15.attr,
376	&hfi1_sl2sc_attr_16.attr,
377	&hfi1_sl2sc_attr_17.attr,
378	&hfi1_sl2sc_attr_18.attr,
379	&hfi1_sl2sc_attr_19.attr,
380	&hfi1_sl2sc_attr_20.attr,
381	&hfi1_sl2sc_attr_21.attr,
382	&hfi1_sl2sc_attr_22.attr,
383	&hfi1_sl2sc_attr_23.attr,
384	&hfi1_sl2sc_attr_24.attr,
385	&hfi1_sl2sc_attr_25.attr,
386	&hfi1_sl2sc_attr_26.attr,
387	&hfi1_sl2sc_attr_27.attr,
388	&hfi1_sl2sc_attr_28.attr,
389	&hfi1_sl2sc_attr_29.attr,
390	&hfi1_sl2sc_attr_30.attr,
391	&hfi1_sl2sc_attr_31.attr,
392	NULL
393};
394
395static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
396			       char *buf)
397{
398	struct hfi1_sl2sc_attr *sattr =
399		container_of(attr, struct hfi1_sl2sc_attr, attr);
400	struct hfi1_pportdata *ppd =
401		container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
402	struct hfi1_ibport *ibp = &ppd->ibport_data;
403
404	return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
405}
406
407static const struct sysfs_ops hfi1_sl2sc_ops = {
408	.show = sl2sc_attr_show,
409};
410
411static struct kobj_type hfi1_sl2sc_ktype = {
412	.release = port_release,
413	.sysfs_ops = &hfi1_sl2sc_ops,
414	.default_attrs = sl2sc_default_attributes
415};
416
417/* End sl2sc */
418
419/* Start vl2mtu */
420
421#define HFI1_VL2MTU_ATTR(N) \
422	static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = { \
423		.attr = { .name = __stringify(N), .mode = 0444 }, \
424		.vl = N						  \
425	}
426
427struct hfi1_vl2mtu_attr {
428	struct attribute attr;
429	int vl;
430};
431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432HFI1_VL2MTU_ATTR(0);
433HFI1_VL2MTU_ATTR(1);
434HFI1_VL2MTU_ATTR(2);
435HFI1_VL2MTU_ATTR(3);
436HFI1_VL2MTU_ATTR(4);
437HFI1_VL2MTU_ATTR(5);
438HFI1_VL2MTU_ATTR(6);
439HFI1_VL2MTU_ATTR(7);
440HFI1_VL2MTU_ATTR(8);
441HFI1_VL2MTU_ATTR(9);
442HFI1_VL2MTU_ATTR(10);
443HFI1_VL2MTU_ATTR(11);
444HFI1_VL2MTU_ATTR(12);
445HFI1_VL2MTU_ATTR(13);
446HFI1_VL2MTU_ATTR(14);
447HFI1_VL2MTU_ATTR(15);
448
449static struct attribute *vl2mtu_default_attributes[] = {
450	&hfi1_vl2mtu_attr_0.attr,
451	&hfi1_vl2mtu_attr_1.attr,
452	&hfi1_vl2mtu_attr_2.attr,
453	&hfi1_vl2mtu_attr_3.attr,
454	&hfi1_vl2mtu_attr_4.attr,
455	&hfi1_vl2mtu_attr_5.attr,
456	&hfi1_vl2mtu_attr_6.attr,
457	&hfi1_vl2mtu_attr_7.attr,
458	&hfi1_vl2mtu_attr_8.attr,
459	&hfi1_vl2mtu_attr_9.attr,
460	&hfi1_vl2mtu_attr_10.attr,
461	&hfi1_vl2mtu_attr_11.attr,
462	&hfi1_vl2mtu_attr_12.attr,
463	&hfi1_vl2mtu_attr_13.attr,
464	&hfi1_vl2mtu_attr_14.attr,
465	&hfi1_vl2mtu_attr_15.attr,
466	NULL
467};
468
469static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
470				char *buf)
471{
472	struct hfi1_vl2mtu_attr *vlattr =
473		container_of(attr, struct hfi1_vl2mtu_attr, attr);
474	struct hfi1_pportdata *ppd =
475		container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
476	struct hfi1_devdata *dd = ppd->dd;
477
478	return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
479}
480
481static const struct sysfs_ops hfi1_vl2mtu_ops = {
482	.show = vl2mtu_attr_show,
483};
484
485static struct kobj_type hfi1_vl2mtu_ktype = {
486	.release = port_release,
487	.sysfs_ops = &hfi1_vl2mtu_ops,
488	.default_attrs = vl2mtu_default_attributes
489};
490
491/* end of per-port file structures and support code */
492
493/*
494 * Start of per-unit (or driver, in some cases, but replicated
495 * per unit) functions (these get a device *)
496 */
497static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
498			   char *buf)
499{
500	struct hfi1_ibdev *dev =
501		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
502
503	return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
504}
505static DEVICE_ATTR_RO(hw_rev);
506
507static ssize_t board_id_show(struct device *device,
508			     struct device_attribute *attr, char *buf)
509{
510	struct hfi1_ibdev *dev =
511		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
512	struct hfi1_devdata *dd = dd_from_dev(dev);
513	int ret;
514
515	if (!dd->boardname)
516		ret = -EINVAL;
517	else
518		ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
519	return ret;
520}
521static DEVICE_ATTR_RO(board_id);
522
523static ssize_t boardversion_show(struct device *device,
524				 struct device_attribute *attr, char *buf)
525{
526	struct hfi1_ibdev *dev =
527		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
528	struct hfi1_devdata *dd = dd_from_dev(dev);
529
530	/* The string printed here is already newline-terminated. */
531	return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
532}
533static DEVICE_ATTR_RO(boardversion);
534
535static ssize_t nctxts_show(struct device *device,
536			   struct device_attribute *attr, char *buf)
537{
538	struct hfi1_ibdev *dev =
539		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
540	struct hfi1_devdata *dd = dd_from_dev(dev);
541
542	/*
543	 * Return the smaller of send and receive contexts.
544	 * Normally, user level applications would require both a send
545	 * and a receive context, so returning the smaller of the two counts
546	 * give a more accurate picture of total contexts available.
547	 */
548	return scnprintf(buf, PAGE_SIZE, "%u\n",
549			 min(dd->num_user_contexts,
550			     (u32)dd->sc_sizes[SC_USER].count));
551}
552static DEVICE_ATTR_RO(nctxts);
553
554static ssize_t nfreectxts_show(struct device *device,
555			       struct device_attribute *attr, char *buf)
556{
557	struct hfi1_ibdev *dev =
558		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
559	struct hfi1_devdata *dd = dd_from_dev(dev);
560
561	/* Return the number of free user ports (contexts) available. */
562	return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
563}
564static DEVICE_ATTR_RO(nfreectxts);
565
566static ssize_t serial_show(struct device *device,
567			   struct device_attribute *attr, char *buf)
568{
569	struct hfi1_ibdev *dev =
570		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
571	struct hfi1_devdata *dd = dd_from_dev(dev);
572
573	return scnprintf(buf, PAGE_SIZE, "%s", dd->serial);
 
574}
575static DEVICE_ATTR_RO(serial);
576
577static ssize_t chip_reset_store(struct device *device,
578				struct device_attribute *attr, const char *buf,
579				size_t count)
580{
581	struct hfi1_ibdev *dev =
582		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
583	struct hfi1_devdata *dd = dd_from_dev(dev);
584	int ret;
585
586	if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
587		ret = -EINVAL;
588		goto bail;
589	}
590
591	ret = hfi1_reset_device(dd->unit);
592bail:
593	return ret < 0 ? ret : count;
594}
595static DEVICE_ATTR_WO(chip_reset);
596
597/*
598 * Convert the reported temperature from an integer (reported in
599 * units of 0.25C) to a floating point number.
600 */
601#define temp2str(temp, buf, size, idx)					\
602	scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ",		\
603			      ((temp) >> 2), ((temp) & 0x3) * 25)
604
605/*
606 * Dump tempsense values, in decimal, to ease shell-scripts.
607 */
608static ssize_t tempsense_show(struct device *device,
609			      struct device_attribute *attr, char *buf)
610{
611	struct hfi1_ibdev *dev =
612		rdma_device_to_drv_device(device, struct hfi1_ibdev, rdi.ibdev);
613	struct hfi1_devdata *dd = dd_from_dev(dev);
614	struct hfi1_temp temp;
615	int ret;
616
617	ret = hfi1_tempsense_rd(dd, &temp);
618	if (!ret) {
619		int idx = 0;
620
621		idx += temp2str(temp.curr, buf, PAGE_SIZE, idx);
622		idx += temp2str(temp.lo_lim, buf, PAGE_SIZE, idx);
623		idx += temp2str(temp.hi_lim, buf, PAGE_SIZE, idx);
624		idx += temp2str(temp.crit_lim, buf, PAGE_SIZE, idx);
625		idx += scnprintf(buf + idx, PAGE_SIZE - idx,
626				"%u %u %u\n", temp.triggers & 0x1,
627				temp.triggers & 0x2, temp.triggers & 0x4);
628		ret = idx;
629	}
630	return ret;
631}
632static DEVICE_ATTR_RO(tempsense);
633
634/*
635 * end of per-unit (or driver, in some cases, but replicated
636 * per unit) functions
637 */
638
639/* start of per-unit file structures and support code */
640static struct attribute *hfi1_attributes[] = {
641	&dev_attr_hw_rev.attr,
642	&dev_attr_board_id.attr,
643	&dev_attr_nctxts.attr,
644	&dev_attr_nfreectxts.attr,
645	&dev_attr_serial.attr,
646	&dev_attr_boardversion.attr,
647	&dev_attr_tempsense.attr,
648	&dev_attr_chip_reset.attr,
649	NULL,
650};
651
652const struct attribute_group ib_hfi1_attr_group = {
653	.attrs = hfi1_attributes,
654};
655
656int hfi1_create_port_files(struct ib_device *ibdev, u8 port_num,
657			   struct kobject *kobj)
658{
659	struct hfi1_pportdata *ppd;
660	struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
661	int ret;
662
663	if (!port_num || port_num > dd->num_pports) {
664		dd_dev_err(dd,
665			   "Skipping infiniband class with invalid port %u\n",
666			   port_num);
667		return -ENODEV;
668	}
669	ppd = &dd->pport[port_num - 1];
670
671	ret = kobject_init_and_add(&ppd->sc2vl_kobj, &hfi1_sc2vl_ktype, kobj,
672				   "sc2vl");
673	if (ret) {
674		dd_dev_err(dd,
675			   "Skipping sc2vl sysfs info, (err %d) port %u\n",
676			   ret, port_num);
677		/*
678		 * Based on the documentation for kobject_init_and_add(), the
679		 * caller should call kobject_put even if this call fails.
680		 */
681		goto bail_sc2vl;
682	}
683	kobject_uevent(&ppd->sc2vl_kobj, KOBJ_ADD);
684
685	ret = kobject_init_and_add(&ppd->sl2sc_kobj, &hfi1_sl2sc_ktype, kobj,
686				   "sl2sc");
687	if (ret) {
688		dd_dev_err(dd,
689			   "Skipping sl2sc sysfs info, (err %d) port %u\n",
690			   ret, port_num);
691		goto bail_sl2sc;
692	}
693	kobject_uevent(&ppd->sl2sc_kobj, KOBJ_ADD);
694
695	ret = kobject_init_and_add(&ppd->vl2mtu_kobj, &hfi1_vl2mtu_ktype, kobj,
696				   "vl2mtu");
697	if (ret) {
698		dd_dev_err(dd,
699			   "Skipping vl2mtu sysfs info, (err %d) port %u\n",
700			   ret, port_num);
701		goto bail_vl2mtu;
702	}
703	kobject_uevent(&ppd->vl2mtu_kobj, KOBJ_ADD);
704
705	ret = kobject_init_and_add(&ppd->pport_cc_kobj, &port_cc_ktype,
706				   kobj, "CCMgtA");
707	if (ret) {
708		dd_dev_err(dd,
709			   "Skipping Congestion Control sysfs info, (err %d) port %u\n",
710			   ret, port_num);
711		goto bail_cc;
712	}
713
714	kobject_uevent(&ppd->pport_cc_kobj, KOBJ_ADD);
715
716	ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_setting_bin_attr);
717	if (ret) {
718		dd_dev_err(dd,
719			   "Skipping Congestion Control setting sysfs info, (err %d) port %u\n",
720			   ret, port_num);
721		goto bail_cc;
722	}
723
724	ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_table_bin_attr);
725	if (ret) {
726		dd_dev_err(dd,
727			   "Skipping Congestion Control table sysfs info, (err %d) port %u\n",
728			   ret, port_num);
729		goto bail_cc_entry_bin;
730	}
731
732	dd_dev_info(dd,
733		    "Congestion Control Agent enabled for port %d\n",
734		    port_num);
735
736	return 0;
737
738bail_cc_entry_bin:
739	sysfs_remove_bin_file(&ppd->pport_cc_kobj,
740			      &cc_setting_bin_attr);
741bail_cc:
742	kobject_put(&ppd->pport_cc_kobj);
743bail_vl2mtu:
744	kobject_put(&ppd->vl2mtu_kobj);
745bail_sl2sc:
746	kobject_put(&ppd->sl2sc_kobj);
747bail_sc2vl:
748	kobject_put(&ppd->sc2vl_kobj);
749	return ret;
750}
751
752struct sde_attribute {
753	struct attribute attr;
754	ssize_t (*show)(struct sdma_engine *sde, char *buf);
755	ssize_t (*store)(struct sdma_engine *sde, const char *buf, size_t cnt);
756};
757
758static ssize_t sde_show(struct kobject *kobj, struct attribute *attr, char *buf)
759{
760	struct sde_attribute *sde_attr =
761		container_of(attr, struct sde_attribute, attr);
762	struct sdma_engine *sde =
763		container_of(kobj, struct sdma_engine, kobj);
764
765	if (!sde_attr->show)
766		return -EINVAL;
767
768	return sde_attr->show(sde, buf);
769}
770
771static ssize_t sde_store(struct kobject *kobj, struct attribute *attr,
772			 const char *buf, size_t count)
773{
774	struct sde_attribute *sde_attr =
775		container_of(attr, struct sde_attribute, attr);
776	struct sdma_engine *sde =
777		container_of(kobj, struct sdma_engine, kobj);
778
779	if (!capable(CAP_SYS_ADMIN))
780		return -EPERM;
781
782	if (!sde_attr->store)
783		return -EINVAL;
784
785	return sde_attr->store(sde, buf, count);
786}
787
788static const struct sysfs_ops sde_sysfs_ops = {
789	.show = sde_show,
790	.store = sde_store,
791};
792
793static struct kobj_type sde_ktype = {
794	.sysfs_ops = &sde_sysfs_ops,
795};
796
797#define SDE_ATTR(_name, _mode, _show, _store) \
798	struct sde_attribute sde_attr_##_name = \
799		__ATTR(_name, _mode, _show, _store)
800
801static ssize_t sde_show_cpu_to_sde_map(struct sdma_engine *sde, char *buf)
802{
803	return sdma_get_cpu_to_sde_map(sde, buf);
804}
805
806static ssize_t sde_store_cpu_to_sde_map(struct sdma_engine *sde,
807					const char *buf, size_t count)
808{
809	return sdma_set_cpu_to_sde_map(sde, buf, count);
810}
811
812static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
813{
814	int vl;
815
816	vl = sdma_engine_get_vl(sde);
817	if (vl < 0)
818		return vl;
819
820	return snprintf(buf, PAGE_SIZE, "%d\n", vl);
821}
822
823static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
824		sde_show_cpu_to_sde_map,
825		sde_store_cpu_to_sde_map);
826static SDE_ATTR(vl, S_IRUGO, sde_show_vl, NULL);
827
828static struct sde_attribute *sde_attribs[] = {
829	&sde_attr_cpu_list,
830	&sde_attr_vl
831};
832
833/*
834 * Register and create our files in /sys/class/infiniband.
835 */
836int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd)
837{
838	struct ib_device *dev = &dd->verbs_dev.rdi.ibdev;
839	struct device *class_dev = &dev->dev;
840	int i, j, ret;
841
842	for (i = 0; i < dd->num_sdma; i++) {
843		ret = kobject_init_and_add(&dd->per_sdma[i].kobj,
844					   &sde_ktype, &class_dev->kobj,
845					   "sdma%d", i);
846		if (ret)
847			goto bail;
848
849		for (j = 0; j < ARRAY_SIZE(sde_attribs); j++) {
850			ret = sysfs_create_file(&dd->per_sdma[i].kobj,
851						&sde_attribs[j]->attr);
852			if (ret)
853				goto bail;
854		}
855	}
856
857	return 0;
858bail:
859	/*
860	 * The function kobject_put() will call kobject_del() if the kobject
861	 * has been added successfully. The sysfs files created under the
862	 * kobject directory will also be removed during the process.
863	 */
864	for (; i >= 0; i--)
865		kobject_put(&dd->per_sdma[i].kobj);
866
867	return ret;
868}
869
870/*
871 * Unregister and remove our files in /sys/class/infiniband.
872 */
873void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd)
874{
875	struct hfi1_pportdata *ppd;
876	int i;
877
878	/* Unwind operations in hfi1_verbs_register_sysfs() */
879	for (i = 0; i < dd->num_sdma; i++)
880		kobject_put(&dd->per_sdma[i].kobj);
881
882	for (i = 0; i < dd->num_pports; i++) {
883		ppd = &dd->pport[i];
884
885		sysfs_remove_bin_file(&ppd->pport_cc_kobj,
886				      &cc_setting_bin_attr);
887		sysfs_remove_bin_file(&ppd->pport_cc_kobj,
888				      &cc_table_bin_attr);
889		kobject_put(&ppd->pport_cc_kobj);
890		kobject_put(&ppd->vl2mtu_kobj);
891		kobject_put(&ppd->sl2sc_kobj);
892		kobject_put(&ppd->sc2vl_kobj);
893	}
894}