Linux Audio

Check our new training course

Loading...
v6.8
  1/*
  2 * Copyright (c) 2012 Intel Corporation.  All rights reserved.
  3 * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
  4 * Copyright (c) 2006 PathScale, 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#include <linux/ctype.h>
 35#include <rdma/ib_sysfs.h>
 36
 37#include "qib.h"
 38#include "qib_mad.h"
 39
 40static struct qib_pportdata *qib_get_pportdata_kobj(struct kobject *kobj)
 
 
 
 
 
 
 
 41{
 42	u32 port_num;
 43	struct ib_device *ibdev = ib_port_sysfs_get_ibdev_kobj(kobj, &port_num);
 44	struct qib_devdata *dd = dd_from_ibdev(ibdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45
 46	return &dd->pport[port_num - 1];
 
 
 
 
 
 47}
 48
 
 49/*
 50 * Get/Set heartbeat enable. OR of 1=enabled, 2=auto
 51 */
 52static ssize_t hrtbt_enable_show(struct ib_device *ibdev, u32 port_num,
 53				 struct ib_port_attribute *attr, char *buf)
 54{
 55	struct qib_devdata *dd = dd_from_ibdev(ibdev);
 56	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
 57
 58	return sysfs_emit(buf, "%d\n", dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT));
 
 
 59}
 60
 61static ssize_t hrtbt_enable_store(struct ib_device *ibdev, u32 port_num,
 62				  struct ib_port_attribute *attr,
 63				  const char *buf, size_t count)
 64{
 65	struct qib_devdata *dd = dd_from_ibdev(ibdev);
 66	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
 67	int ret;
 68	u16 val;
 69
 70	ret = kstrtou16(buf, 0, &val);
 71	if (ret) {
 72		qib_dev_err(dd, "attempt to set invalid Heartbeat enable\n");
 73		return ret;
 74	}
 75
 76	/*
 77	 * Set the "intentional" heartbeat enable per either of
 78	 * "Enable" and "Auto", as these are normally set together.
 79	 * This bit is consulted when leaving loopback mode,
 80	 * because entering loopback mode overrides it and automatically
 81	 * disables heartbeat.
 82	 */
 83	ret = dd->f_set_ib_cfg(ppd, QIB_IB_CFG_HRTBT, val);
 
 
 
 84	return ret < 0 ? ret : count;
 85}
 86static IB_PORT_ATTR_RW(hrtbt_enable);
 87
 88static ssize_t loopback_store(struct ib_device *ibdev, u32 port_num,
 89			      struct ib_port_attribute *attr, const char *buf,
 90			      size_t count)
 91{
 92	struct qib_devdata *dd = dd_from_ibdev(ibdev);
 93	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
 94	int ret = count, r;
 95
 96	r = dd->f_set_ib_loopback(ppd, buf);
 97	if (r < 0)
 98		ret = r;
 99
100	return ret;
101}
102static IB_PORT_ATTR_WO(loopback);
103
104static ssize_t led_override_store(struct ib_device *ibdev, u32 port_num,
105				  struct ib_port_attribute *attr,
106				  const char *buf, size_t count)
107{
108	struct qib_devdata *dd = dd_from_ibdev(ibdev);
109	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
110	int ret;
111	u16 val;
112
113	ret = kstrtou16(buf, 0, &val);
114	if (ret) {
 
 
115		qib_dev_err(dd, "attempt to set invalid LED override\n");
116		return ret;
117	}
118
119	qib_set_led_override(ppd, val);
120	return count;
121}
122static IB_PORT_ATTR_WO(led_override);
123
124static ssize_t status_show(struct ib_device *ibdev, u32 port_num,
125			   struct ib_port_attribute *attr, char *buf)
126{
127	struct qib_devdata *dd = dd_from_ibdev(ibdev);
128	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
129
130	if (!ppd->statusp)
131		return -EINVAL;
132
133	return sysfs_emit(buf, "0x%llx\n", (unsigned long long)*(ppd->statusp));
 
 
134}
135static IB_PORT_ATTR_RO(status);
136
137/*
138 * For userland compatibility, these offsets must remain fixed.
139 * They are strings for QIB_STATUS_*
140 */
141static const char * const qib_status_str[] = {
142	"Initted",
143	"",
144	"",
145	"",
146	"",
147	"Present",
148	"IB_link_up",
149	"IB_configured",
150	"",
151	"Fatal_Hardware_Error",
152	NULL,
153};
154
155static ssize_t status_str_show(struct ib_device *ibdev, u32 port_num,
156			       struct ib_port_attribute *attr, char *buf)
157{
158	struct qib_devdata *dd = dd_from_ibdev(ibdev);
159	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
160	int i, any;
161	u64 s;
162	ssize_t ret;
163
164	if (!ppd->statusp) {
165		ret = -EINVAL;
166		goto bail;
167	}
168
169	s = *(ppd->statusp);
170	*buf = '\0';
171	for (any = i = 0; s && qib_status_str[i]; i++) {
172		if (s & 1) {
173			/* if overflow */
174			if (any && strlcat(buf, " ", PAGE_SIZE) >= PAGE_SIZE)
175				break;
176			if (strlcat(buf, qib_status_str[i], PAGE_SIZE) >=
177					PAGE_SIZE)
178				break;
179			any = 1;
180		}
181		s >>= 1;
182	}
183	if (any)
184		strlcat(buf, "\n", PAGE_SIZE);
185
186	ret = strlen(buf);
187
188bail:
189	return ret;
190}
191static IB_PORT_ATTR_RO(status_str);
192
193/* end of per-port functions */
194
195static struct attribute *port_linkcontrol_attributes[] = {
196	&ib_port_attr_loopback.attr,
197	&ib_port_attr_led_override.attr,
198	&ib_port_attr_hrtbt_enable.attr,
199	&ib_port_attr_status.attr,
200	&ib_port_attr_status_str.attr,
201	NULL
202};
203
204static const struct attribute_group port_linkcontrol_group = {
205	.name = "linkcontrol",
206	.attrs = port_linkcontrol_attributes,
207};
208
209/*
210 * Start of per-port congestion control structures and support code
 
 
211 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
213/*
214 * Congestion control table size followed by table entries
215 */
216static ssize_t cc_table_bin_read(struct file *filp, struct kobject *kobj,
217				 struct bin_attribute *bin_attr, char *buf,
218				 loff_t pos, size_t count)
219{
220	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
221	int ret;
222
223	if (!qib_cc_table_size || !ppd->ccti_entries_shadow)
224		return -EINVAL;
225
226	ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
227		 + sizeof(__be16);
228
229	if (pos > ret)
230		return -EINVAL;
231
232	if (count > ret - pos)
233		count = ret - pos;
234
235	if (!count)
236		return count;
237
238	spin_lock(&ppd->cc_shadow_lock);
239	memcpy(buf, ppd->ccti_entries_shadow, count);
240	spin_unlock(&ppd->cc_shadow_lock);
241
242	return count;
243}
244static BIN_ATTR_RO(cc_table_bin, PAGE_SIZE);
245
246/*
247 * Congestion settings: port control, control map and an array of 16
248 * entries for the congestion entries - increase, timer, event log
249 * trigger threshold and the minimum injection rate delay.
250 */
251static ssize_t cc_setting_bin_read(struct file *filp, struct kobject *kobj,
252				   struct bin_attribute *bin_attr, char *buf,
253				   loff_t pos, size_t count)
254{
255	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
256	int ret;
257
258	if (!qib_cc_table_size || !ppd->congestion_entries_shadow)
259		return -EINVAL;
260
261	ret = sizeof(struct ib_cc_congestion_setting_attr_shadow);
262
263	if (pos > ret)
264		return -EINVAL;
265	if (count > ret - pos)
266		count = ret - pos;
267
268	if (!count)
269		return count;
270
271	spin_lock(&ppd->cc_shadow_lock);
272	memcpy(buf, ppd->congestion_entries_shadow, count);
273	spin_unlock(&ppd->cc_shadow_lock);
274
275	return count;
276}
277static BIN_ATTR_RO(cc_setting_bin, PAGE_SIZE);
278
279static struct bin_attribute *port_ccmgta_attributes[] = {
280	&bin_attr_cc_setting_bin,
281	&bin_attr_cc_table_bin,
282	NULL,
283};
284
285static umode_t qib_ccmgta_is_bin_visible(struct kobject *kobj,
286				 struct bin_attribute *attr, int n)
287{
288	struct qib_pportdata *ppd = qib_get_pportdata_kobj(kobj);
289
290	if (!qib_cc_table_size || !ppd->congestion_entries_shadow)
291		return 0;
292	return attr->attr.mode;
293}
294
295static const struct attribute_group port_ccmgta_attribute_group = {
296	.name = "CCMgtA",
297	.is_bin_visible = qib_ccmgta_is_bin_visible,
298	.bin_attrs = port_ccmgta_attributes,
 
 
 
 
 
299};
300
301/* Start sl2vl */
302
 
 
 
 
 
 
303struct qib_sl2vl_attr {
304	struct ib_port_attribute attr;
305	int sl;
306};
307
308static ssize_t sl2vl_attr_show(struct ib_device *ibdev, u32 port_num,
309			       struct ib_port_attribute *attr, char *buf)
310{
311	struct qib_sl2vl_attr *sattr =
312		container_of(attr, struct qib_sl2vl_attr, attr);
313	struct qib_devdata *dd = dd_from_ibdev(ibdev);
314	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
315
316	return sysfs_emit(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
317}
318
319#define QIB_SL2VL_ATTR(N)                                                      \
320	static struct qib_sl2vl_attr qib_sl2vl_attr_##N = {                    \
321		.attr = __ATTR(N, 0444, sl2vl_attr_show, NULL),                \
322		.sl = N,                                                       \
323	}
324
325QIB_SL2VL_ATTR(0);
326QIB_SL2VL_ATTR(1);
327QIB_SL2VL_ATTR(2);
328QIB_SL2VL_ATTR(3);
329QIB_SL2VL_ATTR(4);
330QIB_SL2VL_ATTR(5);
331QIB_SL2VL_ATTR(6);
332QIB_SL2VL_ATTR(7);
333QIB_SL2VL_ATTR(8);
334QIB_SL2VL_ATTR(9);
335QIB_SL2VL_ATTR(10);
336QIB_SL2VL_ATTR(11);
337QIB_SL2VL_ATTR(12);
338QIB_SL2VL_ATTR(13);
339QIB_SL2VL_ATTR(14);
340QIB_SL2VL_ATTR(15);
341
342static struct attribute *port_sl2vl_attributes[] = {
343	&qib_sl2vl_attr_0.attr.attr,
344	&qib_sl2vl_attr_1.attr.attr,
345	&qib_sl2vl_attr_2.attr.attr,
346	&qib_sl2vl_attr_3.attr.attr,
347	&qib_sl2vl_attr_4.attr.attr,
348	&qib_sl2vl_attr_5.attr.attr,
349	&qib_sl2vl_attr_6.attr.attr,
350	&qib_sl2vl_attr_7.attr.attr,
351	&qib_sl2vl_attr_8.attr.attr,
352	&qib_sl2vl_attr_9.attr.attr,
353	&qib_sl2vl_attr_10.attr.attr,
354	&qib_sl2vl_attr_11.attr.attr,
355	&qib_sl2vl_attr_12.attr.attr,
356	&qib_sl2vl_attr_13.attr.attr,
357	&qib_sl2vl_attr_14.attr.attr,
358	&qib_sl2vl_attr_15.attr.attr,
359	NULL
360};
361
362static const struct attribute_group port_sl2vl_group = {
363	.name = "sl2vl",
364	.attrs = port_sl2vl_attributes,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
365};
366
367/* End sl2vl */
368
369/* Start diag_counters */
370
 
 
 
 
 
 
371struct qib_diagc_attr {
372	struct ib_port_attribute attr;
373	size_t counter;
374};
375
376static ssize_t diagc_attr_show(struct ib_device *ibdev, u32 port_num,
377			       struct ib_port_attribute *attr, char *buf)
378{
379	struct qib_diagc_attr *dattr =
380		container_of(attr, struct qib_diagc_attr, attr);
381	struct qib_devdata *dd = dd_from_ibdev(ibdev);
382	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
383
384	return sysfs_emit(buf, "%llu\n", *((u64 *)qibp + dattr->counter));
385}
386
387static ssize_t diagc_attr_store(struct ib_device *ibdev, u32 port_num,
388				struct ib_port_attribute *attr, const char *buf,
389				size_t count)
390{
391	struct qib_diagc_attr *dattr =
392		container_of(attr, struct qib_diagc_attr, attr);
393	struct qib_devdata *dd = dd_from_ibdev(ibdev);
394	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
395	u64 val;
396	int ret;
397
398	ret = kstrtou64(buf, 0, &val);
399	if (ret)
400		return ret;
401	*((u64 *)qibp + dattr->counter) = val;
402	return count;
403}
404
405#define QIB_DIAGC_ATTR(N)                                                      \
406	static_assert(__same_type(((struct qib_ibport *)0)->rvp.n_##N, u64));  \
407	static struct qib_diagc_attr qib_diagc_attr_##N = {                    \
408		.attr = __ATTR(N, 0664, diagc_attr_show, diagc_attr_store),    \
409		.counter =                                                     \
410			offsetof(struct qib_ibport, rvp.n_##N) / sizeof(u64)   \
411	}
412
413QIB_DIAGC_ATTR(rc_resends);
 
 
 
414QIB_DIAGC_ATTR(seq_naks);
415QIB_DIAGC_ATTR(rdma_seq);
416QIB_DIAGC_ATTR(rnr_naks);
417QIB_DIAGC_ATTR(other_naks);
418QIB_DIAGC_ATTR(rc_timeouts);
419QIB_DIAGC_ATTR(loop_pkts);
420QIB_DIAGC_ATTR(pkt_drops);
421QIB_DIAGC_ATTR(dmawait);
422QIB_DIAGC_ATTR(unaligned);
423QIB_DIAGC_ATTR(rc_dupreq);
424QIB_DIAGC_ATTR(rc_seqnak);
425QIB_DIAGC_ATTR(rc_crwaits);
426
427static u64 get_all_cpu_total(u64 __percpu *cntr)
428{
429	int cpu;
430	u64 counter = 0;
431
432	for_each_possible_cpu(cpu)
433		counter += *per_cpu_ptr(cntr, cpu);
434	return counter;
435}
436
437static ssize_t qib_store_per_cpu(struct qib_devdata *dd, const char *buf,
438				 size_t count, u64 *zero, u64 cur)
439{
440	u32 val;
441	int ret;
442
443	ret = kstrtou32(buf, 0, &val);
444	if (ret)
445		return ret;
446	if (val != 0) {
447		qib_dev_err(dd, "Per CPU cntrs can only be zeroed");
448		return count;
449	}
450	*zero = cur;
451	return count;
452}
453
454static ssize_t rc_acks_show(struct ib_device *ibdev, u32 port_num,
455			    struct ib_port_attribute *attr, char *buf)
456{
457	struct qib_devdata *dd = dd_from_ibdev(ibdev);
458	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
459
460	return sysfs_emit(buf, "%llu\n",
461			  get_all_cpu_total(qibp->rvp.rc_acks) -
462				  qibp->rvp.z_rc_acks);
463}
464
465static ssize_t rc_acks_store(struct ib_device *ibdev, u32 port_num,
466			     struct ib_port_attribute *attr, const char *buf,
467			     size_t count)
468{
469	struct qib_devdata *dd = dd_from_ibdev(ibdev);
470	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
471
472	return qib_store_per_cpu(dd, buf, count, &qibp->rvp.z_rc_acks,
473				 get_all_cpu_total(qibp->rvp.rc_acks));
474}
475static IB_PORT_ATTR_RW(rc_acks);
476
477static ssize_t rc_qacks_show(struct ib_device *ibdev, u32 port_num,
478			     struct ib_port_attribute *attr, char *buf)
479{
480	struct qib_devdata *dd = dd_from_ibdev(ibdev);
481	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
482
483	return sysfs_emit(buf, "%llu\n",
484			  get_all_cpu_total(qibp->rvp.rc_qacks) -
485				  qibp->rvp.z_rc_qacks);
486}
 
 
 
487
488static ssize_t rc_qacks_store(struct ib_device *ibdev, u32 port_num,
489			      struct ib_port_attribute *attr, const char *buf,
490			      size_t count)
491{
492	struct qib_devdata *dd = dd_from_ibdev(ibdev);
493	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
 
 
 
494
495	return qib_store_per_cpu(dd, buf, count, &qibp->rvp.z_rc_qacks,
496				 get_all_cpu_total(qibp->rvp.rc_qacks));
497}
498static IB_PORT_ATTR_RW(rc_qacks);
499
500static ssize_t rc_delayed_comp_show(struct ib_device *ibdev, u32 port_num,
501				    struct ib_port_attribute *attr, char *buf)
502{
503	struct qib_devdata *dd = dd_from_ibdev(ibdev);
504	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
505
506	return sysfs_emit(buf, "%llu\n",
507			 get_all_cpu_total(qibp->rvp.rc_delayed_comp) -
508				 qibp->rvp.z_rc_delayed_comp);
509}
510
511static ssize_t rc_delayed_comp_store(struct ib_device *ibdev, u32 port_num,
512				     struct ib_port_attribute *attr,
513				     const char *buf, size_t count)
514{
515	struct qib_devdata *dd = dd_from_ibdev(ibdev);
516	struct qib_ibport *qibp = &dd->pport[port_num - 1].ibport_data;
517
518	return qib_store_per_cpu(dd, buf, count, &qibp->rvp.z_rc_delayed_comp,
519				 get_all_cpu_total(qibp->rvp.rc_delayed_comp));
520}
521static IB_PORT_ATTR_RW(rc_delayed_comp);
522
523static struct attribute *port_diagc_attributes[] = {
524	&qib_diagc_attr_rc_resends.attr.attr,
525	&qib_diagc_attr_seq_naks.attr.attr,
526	&qib_diagc_attr_rdma_seq.attr.attr,
527	&qib_diagc_attr_rnr_naks.attr.attr,
528	&qib_diagc_attr_other_naks.attr.attr,
529	&qib_diagc_attr_rc_timeouts.attr.attr,
530	&qib_diagc_attr_loop_pkts.attr.attr,
531	&qib_diagc_attr_pkt_drops.attr.attr,
532	&qib_diagc_attr_dmawait.attr.attr,
533	&qib_diagc_attr_unaligned.attr.attr,
534	&qib_diagc_attr_rc_dupreq.attr.attr,
535	&qib_diagc_attr_rc_seqnak.attr.attr,
536	&qib_diagc_attr_rc_crwaits.attr.attr,
537	&ib_port_attr_rc_acks.attr,
538	&ib_port_attr_rc_qacks.attr,
539	&ib_port_attr_rc_delayed_comp.attr,
540	NULL
541};
542
543static const struct attribute_group port_diagc_group = {
544	.name = "diag_counters",
545	.attrs = port_diagc_attributes,
 
546};
547
548/* End diag_counters */
549
550const struct attribute_group *qib_attr_port_groups[] = {
551	&port_linkcontrol_group,
552	&port_ccmgta_attribute_group,
553	&port_sl2vl_group,
554	&port_diagc_group,
555	NULL,
556};
557
558/* end of per-port file structures and support code */
559
560/*
561 * Start of per-unit (or driver, in some cases, but replicated
562 * per unit) functions (these get a device *)
563 */
564static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
565			   char *buf)
566{
567	struct qib_ibdev *dev =
568		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
569
570	return sysfs_emit(buf, "%x\n", dd_from_dev(dev)->minrev);
571}
572static DEVICE_ATTR_RO(hw_rev);
573
574static ssize_t hca_type_show(struct device *device,
575			     struct device_attribute *attr, char *buf)
576{
577	struct qib_ibdev *dev =
578		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
579	struct qib_devdata *dd = dd_from_dev(dev);
 
580
581	if (!dd->boardname)
582		return -EINVAL;
583	return sysfs_emit(buf, "%s\n", dd->boardname);
 
 
584}
585static DEVICE_ATTR_RO(hca_type);
586static DEVICE_ATTR(board_id, 0444, hca_type_show, NULL);
587
588static ssize_t version_show(struct device *device,
589			    struct device_attribute *attr, char *buf)
590{
591	/* The string printed here is already newline-terminated. */
592	return sysfs_emit(buf, "%s", (char *)ib_qib_version);
593}
594static DEVICE_ATTR_RO(version);
595
596static ssize_t boardversion_show(struct device *device,
597				 struct device_attribute *attr, char *buf)
598{
599	struct qib_ibdev *dev =
600		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
601	struct qib_devdata *dd = dd_from_dev(dev);
602
603	/* The string printed here is already newline-terminated. */
604	return sysfs_emit(buf, "%s", dd->boardversion);
605}
606static DEVICE_ATTR_RO(boardversion);
607
608static ssize_t localbus_info_show(struct device *device,
 
609				  struct device_attribute *attr, char *buf)
610{
611	struct qib_ibdev *dev =
612		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
613	struct qib_devdata *dd = dd_from_dev(dev);
614
615	/* The string printed here is already newline-terminated. */
616	return sysfs_emit(buf, "%s", dd->lbus_info);
617}
618static DEVICE_ATTR_RO(localbus_info);
619
620static ssize_t nctxts_show(struct device *device,
 
621			   struct device_attribute *attr, char *buf)
622{
623	struct qib_ibdev *dev =
624		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
625	struct qib_devdata *dd = dd_from_dev(dev);
626
627	/* Return the number of user ports (contexts) available. */
628	/* The calculation below deals with a special case where
629	 * cfgctxts is set to 1 on a single-port board. */
630	return sysfs_emit(buf, "%u\n",
631			  (dd->first_user_ctxt > dd->cfgctxts) ?
632				  0 :
633				  (dd->cfgctxts - dd->first_user_ctxt));
634}
635static DEVICE_ATTR_RO(nctxts);
636
637static ssize_t nfreectxts_show(struct device *device,
638			       struct device_attribute *attr, char *buf)
639{
640	struct qib_ibdev *dev =
641		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
642	struct qib_devdata *dd = dd_from_dev(dev);
643
644	/* Return the number of free user ports (contexts) available. */
645	return sysfs_emit(buf, "%u\n", dd->freectxts);
 
646}
647static DEVICE_ATTR_RO(nfreectxts);
648
649static ssize_t serial_show(struct device *device, struct device_attribute *attr,
650			   char *buf)
651{
652	struct qib_ibdev *dev =
653		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
654	struct qib_devdata *dd = dd_from_dev(dev);
655	const u8 *end = memchr(dd->serial, 0, ARRAY_SIZE(dd->serial));
656	int size = end ? end - dd->serial : ARRAY_SIZE(dd->serial);
657
658	return sysfs_emit(buf, ".%*s\n", size, dd->serial);
 
 
 
659}
660static DEVICE_ATTR_RO(serial);
661
662static ssize_t chip_reset_store(struct device *device,
663				struct device_attribute *attr, const char *buf,
664				size_t count)
665{
666	struct qib_ibdev *dev =
667		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
668	struct qib_devdata *dd = dd_from_dev(dev);
669	int ret;
670
671	if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
672		ret = -EINVAL;
673		goto bail;
674	}
675
676	ret = qib_reset_device(dd->unit);
677bail:
678	return ret < 0 ? ret : count;
679}
680static DEVICE_ATTR_WO(chip_reset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681
682/*
683 * Dump tempsense regs. in decimal, to ease shell-scripts.
684 */
685static ssize_t tempsense_show(struct device *device,
686			      struct device_attribute *attr, char *buf)
687{
688	struct qib_ibdev *dev =
689		rdma_device_to_drv_device(device, struct qib_ibdev, rdi.ibdev);
690	struct qib_devdata *dd = dd_from_dev(dev);
691	int i;
 
692	u8 regvals[8];
693
694	for (i = 0; i < 8; i++) {
695		int ret;
696
697		if (i == 6)
698			continue;
699		ret = dd->f_tempsense_rd(dd, i);
700		if (ret < 0)
701			return ret;	/* return error on bad read */
702		regvals[i] = ret;
703	}
704	return sysfs_emit(buf, "%d %d %02X %02X %d %d\n",
705			  (signed char)regvals[0],
706			  (signed char)regvals[1],
707			  regvals[2],
708			  regvals[3],
709			  (signed char)regvals[5],
710			  (signed char)regvals[7]);
 
711}
712static DEVICE_ATTR_RO(tempsense);
713
714/*
715 * end of per-unit (or driver, in some cases, but replicated
716 * per unit) functions
717 */
718
719/* start of per-unit file structures and support code */
720static struct attribute *qib_attributes[] = {
721	&dev_attr_hw_rev.attr,
722	&dev_attr_hca_type.attr,
723	&dev_attr_board_id.attr,
724	&dev_attr_version.attr,
725	&dev_attr_nctxts.attr,
726	&dev_attr_nfreectxts.attr,
727	&dev_attr_serial.attr,
728	&dev_attr_boardversion.attr,
729	&dev_attr_tempsense.attr,
730	&dev_attr_localbus_info.attr,
731	&dev_attr_chip_reset.attr,
732	NULL,
 
 
 
 
 
 
 
 
 
 
 
 
 
733};
734
735const struct attribute_group qib_attr_group = {
736	.attrs = qib_attributes,
737};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v3.1
  1/*
  2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
 
  3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
  4 *
  5 * This software is available to you under a choice of one of two
  6 * licenses.  You may choose to be licensed under the terms of the GNU
  7 * General Public License (GPL) Version 2, available from the file
  8 * COPYING in the main directory of this source tree, or the
  9 * OpenIB.org BSD license below:
 10 *
 11 *     Redistribution and use in source and binary forms, with or
 12 *     without modification, are permitted provided that the following
 13 *     conditions are met:
 14 *
 15 *      - Redistributions of source code must retain the above
 16 *        copyright notice, this list of conditions and the following
 17 *        disclaimer.
 18 *
 19 *      - Redistributions in binary form must reproduce the above
 20 *        copyright notice, this list of conditions and the following
 21 *        disclaimer in the documentation and/or other materials
 22 *        provided with the distribution.
 23 *
 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 31 * SOFTWARE.
 32 */
 33#include <linux/ctype.h>
 
 34
 35#include "qib.h"
 
 36
 37/**
 38 * qib_parse_ushort - parse an unsigned short value in an arbitrary base
 39 * @str: the string containing the number
 40 * @valp: where to put the result
 41 *
 42 * Returns the number of bytes consumed, or negative value on error.
 43 */
 44static int qib_parse_ushort(const char *str, unsigned short *valp)
 45{
 46	unsigned long val;
 47	char *end;
 48	int ret;
 49
 50	if (!isdigit(str[0])) {
 51		ret = -EINVAL;
 52		goto bail;
 53	}
 54
 55	val = simple_strtoul(str, &end, 0);
 56
 57	if (val > 0xffff) {
 58		ret = -EINVAL;
 59		goto bail;
 60	}
 61
 62	*valp = val;
 63
 64	ret = end + 1 - str;
 65	if (ret == 0)
 66		ret = -EINVAL;
 67
 68bail:
 69	return ret;
 70}
 71
 72/* start of per-port functions */
 73/*
 74 * Get/Set heartbeat enable. OR of 1=enabled, 2=auto
 75 */
 76static ssize_t show_hrtbt_enb(struct qib_pportdata *ppd, char *buf)
 
 77{
 78	struct qib_devdata *dd = ppd->dd;
 79	int ret;
 80
 81	ret = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT);
 82	ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
 83	return ret;
 84}
 85
 86static ssize_t store_hrtbt_enb(struct qib_pportdata *ppd, const char *buf,
 87			       size_t count)
 
 88{
 89	struct qib_devdata *dd = ppd->dd;
 
 90	int ret;
 91	u16 val;
 92
 93	ret = qib_parse_ushort(buf, &val);
 
 
 
 
 94
 95	/*
 96	 * Set the "intentional" heartbeat enable per either of
 97	 * "Enable" and "Auto", as these are normally set together.
 98	 * This bit is consulted when leaving loopback mode,
 99	 * because entering loopback mode overrides it and automatically
100	 * disables heartbeat.
101	 */
102	if (ret >= 0)
103		ret = dd->f_set_ib_cfg(ppd, QIB_IB_CFG_HRTBT, val);
104	if (ret < 0)
105		qib_dev_err(dd, "attempt to set invalid Heartbeat enable\n");
106	return ret < 0 ? ret : count;
107}
 
108
109static ssize_t store_loopback(struct qib_pportdata *ppd, const char *buf,
 
110			      size_t count)
111{
112	struct qib_devdata *dd = ppd->dd;
 
113	int ret = count, r;
114
115	r = dd->f_set_ib_loopback(ppd, buf);
116	if (r < 0)
117		ret = r;
118
119	return ret;
120}
 
121
122static ssize_t store_led_override(struct qib_pportdata *ppd, const char *buf,
123				  size_t count)
 
124{
125	struct qib_devdata *dd = ppd->dd;
 
126	int ret;
127	u16 val;
128
129	ret = qib_parse_ushort(buf, &val);
130	if (ret > 0)
131		qib_set_led_override(ppd, val);
132	else
133		qib_dev_err(dd, "attempt to set invalid LED override\n");
134	return ret < 0 ? ret : count;
 
 
 
 
135}
 
136
137static ssize_t show_status(struct qib_pportdata *ppd, char *buf)
 
138{
139	ssize_t ret;
 
140
141	if (!ppd->statusp)
142		ret = -EINVAL;
143	else
144		ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
145				(unsigned long long) *(ppd->statusp));
146	return ret;
147}
 
148
149/*
150 * For userland compatibility, these offsets must remain fixed.
151 * They are strings for QIB_STATUS_*
152 */
153static const char *qib_status_str[] = {
154	"Initted",
155	"",
156	"",
157	"",
158	"",
159	"Present",
160	"IB_link_up",
161	"IB_configured",
162	"",
163	"Fatal_Hardware_Error",
164	NULL,
165};
166
167static ssize_t show_status_str(struct qib_pportdata *ppd, char *buf)
 
168{
 
 
169	int i, any;
170	u64 s;
171	ssize_t ret;
172
173	if (!ppd->statusp) {
174		ret = -EINVAL;
175		goto bail;
176	}
177
178	s = *(ppd->statusp);
179	*buf = '\0';
180	for (any = i = 0; s && qib_status_str[i]; i++) {
181		if (s & 1) {
182			/* if overflow */
183			if (any && strlcat(buf, " ", PAGE_SIZE) >= PAGE_SIZE)
184				break;
185			if (strlcat(buf, qib_status_str[i], PAGE_SIZE) >=
186					PAGE_SIZE)
187				break;
188			any = 1;
189		}
190		s >>= 1;
191	}
192	if (any)
193		strlcat(buf, "\n", PAGE_SIZE);
194
195	ret = strlen(buf);
196
197bail:
198	return ret;
199}
 
200
201/* end of per-port functions */
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203/*
204 * Start of per-port file structures and support code
205 * Because we are fitting into other infrastructure, we have to supply the
206 * full set of kobject/sysfs_ops structures and routines.
207 */
208#define QIB_PORT_ATTR(name, mode, show, store) \
209	static struct qib_port_attr qib_port_attr_##name = \
210		__ATTR(name, mode, show, store)
211
212struct qib_port_attr {
213	struct attribute attr;
214	ssize_t (*show)(struct qib_pportdata *, char *);
215	ssize_t (*store)(struct qib_pportdata *, const char *, size_t);
216};
217
218QIB_PORT_ATTR(loopback, S_IWUSR, NULL, store_loopback);
219QIB_PORT_ATTR(led_override, S_IWUSR, NULL, store_led_override);
220QIB_PORT_ATTR(hrtbt_enable, S_IWUSR | S_IRUGO, show_hrtbt_enb,
221	      store_hrtbt_enb);
222QIB_PORT_ATTR(status, S_IRUGO, show_status, NULL);
223QIB_PORT_ATTR(status_str, S_IRUGO, show_status_str, NULL);
224
225static struct attribute *port_default_attributes[] = {
226	&qib_port_attr_loopback.attr,
227	&qib_port_attr_led_override.attr,
228	&qib_port_attr_hrtbt_enable.attr,
229	&qib_port_attr_status.attr,
230	&qib_port_attr_status_str.attr,
231	NULL
232};
233
234static ssize_t qib_portattr_show(struct kobject *kobj,
235	struct attribute *attr, char *buf)
 
 
 
 
236{
237	struct qib_port_attr *pattr =
238		container_of(attr, struct qib_port_attr, attr);
239	struct qib_pportdata *ppd =
240		container_of(kobj, struct qib_pportdata, pport_kobj);
 
 
 
 
 
 
 
241
242	return pattr->show(ppd, buf);
 
 
 
 
 
 
 
 
 
 
243}
 
244
245static ssize_t qib_portattr_store(struct kobject *kobj,
246	struct attribute *attr, const char *buf, size_t len)
 
 
 
 
 
 
247{
248	struct qib_port_attr *pattr =
249		container_of(attr, struct qib_port_attr, attr);
250	struct qib_pportdata *ppd =
251		container_of(kobj, struct qib_pportdata, pport_kobj);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
253	return pattr->store(ppd, buf, len);
254}
 
 
 
 
 
 
 
255
256static void qib_port_release(struct kobject *kobj)
 
257{
258	/* nothing to do since memory is freed by qib_free_devdata() */
 
 
 
 
259}
260
261static const struct sysfs_ops qib_port_ops = {
262	.show = qib_portattr_show,
263	.store = qib_portattr_store,
264};
265
266static struct kobj_type qib_port_ktype = {
267	.release = qib_port_release,
268	.sysfs_ops = &qib_port_ops,
269	.default_attrs = port_default_attributes
270};
271
272/* Start sl2vl */
273
274#define QIB_SL2VL_ATTR(N) \
275	static struct qib_sl2vl_attr qib_sl2vl_attr_##N = { \
276		.attr = { .name = __stringify(N), .mode = 0444 }, \
277		.sl = N \
278	}
279
280struct qib_sl2vl_attr {
281	struct attribute attr;
282	int sl;
283};
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285QIB_SL2VL_ATTR(0);
286QIB_SL2VL_ATTR(1);
287QIB_SL2VL_ATTR(2);
288QIB_SL2VL_ATTR(3);
289QIB_SL2VL_ATTR(4);
290QIB_SL2VL_ATTR(5);
291QIB_SL2VL_ATTR(6);
292QIB_SL2VL_ATTR(7);
293QIB_SL2VL_ATTR(8);
294QIB_SL2VL_ATTR(9);
295QIB_SL2VL_ATTR(10);
296QIB_SL2VL_ATTR(11);
297QIB_SL2VL_ATTR(12);
298QIB_SL2VL_ATTR(13);
299QIB_SL2VL_ATTR(14);
300QIB_SL2VL_ATTR(15);
301
302static struct attribute *sl2vl_default_attributes[] = {
303	&qib_sl2vl_attr_0.attr,
304	&qib_sl2vl_attr_1.attr,
305	&qib_sl2vl_attr_2.attr,
306	&qib_sl2vl_attr_3.attr,
307	&qib_sl2vl_attr_4.attr,
308	&qib_sl2vl_attr_5.attr,
309	&qib_sl2vl_attr_6.attr,
310	&qib_sl2vl_attr_7.attr,
311	&qib_sl2vl_attr_8.attr,
312	&qib_sl2vl_attr_9.attr,
313	&qib_sl2vl_attr_10.attr,
314	&qib_sl2vl_attr_11.attr,
315	&qib_sl2vl_attr_12.attr,
316	&qib_sl2vl_attr_13.attr,
317	&qib_sl2vl_attr_14.attr,
318	&qib_sl2vl_attr_15.attr,
319	NULL
320};
321
322static ssize_t sl2vl_attr_show(struct kobject *kobj, struct attribute *attr,
323			       char *buf)
324{
325	struct qib_sl2vl_attr *sattr =
326		container_of(attr, struct qib_sl2vl_attr, attr);
327	struct qib_pportdata *ppd =
328		container_of(kobj, struct qib_pportdata, sl2vl_kobj);
329	struct qib_ibport *qibp = &ppd->ibport_data;
330
331	return sprintf(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
332}
333
334static const struct sysfs_ops qib_sl2vl_ops = {
335	.show = sl2vl_attr_show,
336};
337
338static struct kobj_type qib_sl2vl_ktype = {
339	.release = qib_port_release,
340	.sysfs_ops = &qib_sl2vl_ops,
341	.default_attrs = sl2vl_default_attributes
342};
343
344/* End sl2vl */
345
346/* Start diag_counters */
347
348#define QIB_DIAGC_ATTR(N) \
349	static struct qib_diagc_attr qib_diagc_attr_##N = { \
350		.attr = { .name = __stringify(N), .mode = 0664 }, \
351		.counter = offsetof(struct qib_ibport, n_##N) \
352	}
353
354struct qib_diagc_attr {
355	struct attribute attr;
356	size_t counter;
357};
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359QIB_DIAGC_ATTR(rc_resends);
360QIB_DIAGC_ATTR(rc_acks);
361QIB_DIAGC_ATTR(rc_qacks);
362QIB_DIAGC_ATTR(rc_delayed_comp);
363QIB_DIAGC_ATTR(seq_naks);
364QIB_DIAGC_ATTR(rdma_seq);
365QIB_DIAGC_ATTR(rnr_naks);
366QIB_DIAGC_ATTR(other_naks);
367QIB_DIAGC_ATTR(rc_timeouts);
368QIB_DIAGC_ATTR(loop_pkts);
369QIB_DIAGC_ATTR(pkt_drops);
370QIB_DIAGC_ATTR(dmawait);
371QIB_DIAGC_ATTR(unaligned);
372QIB_DIAGC_ATTR(rc_dupreq);
373QIB_DIAGC_ATTR(rc_seqnak);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
375static struct attribute *diagc_default_attributes[] = {
376	&qib_diagc_attr_rc_resends.attr,
377	&qib_diagc_attr_rc_acks.attr,
378	&qib_diagc_attr_rc_qacks.attr,
379	&qib_diagc_attr_rc_delayed_comp.attr,
380	&qib_diagc_attr_seq_naks.attr,
381	&qib_diagc_attr_rdma_seq.attr,
382	&qib_diagc_attr_rnr_naks.attr,
383	&qib_diagc_attr_other_naks.attr,
384	&qib_diagc_attr_rc_timeouts.attr,
385	&qib_diagc_attr_loop_pkts.attr,
386	&qib_diagc_attr_pkt_drops.attr,
387	&qib_diagc_attr_dmawait.attr,
388	&qib_diagc_attr_unaligned.attr,
389	&qib_diagc_attr_rc_dupreq.attr,
390	&qib_diagc_attr_rc_seqnak.attr,
391	NULL
392};
393
394static ssize_t diagc_attr_show(struct kobject *kobj, struct attribute *attr,
395			       char *buf)
 
396{
397	struct qib_diagc_attr *dattr =
398		container_of(attr, struct qib_diagc_attr, attr);
399	struct qib_pportdata *ppd =
400		container_of(kobj, struct qib_pportdata, diagc_kobj);
401	struct qib_ibport *qibp = &ppd->ibport_data;
402
403	return sprintf(buf, "%u\n", *(u32 *)((char *)qibp + dattr->counter));
 
404}
 
405
406static ssize_t diagc_attr_store(struct kobject *kobj, struct attribute *attr,
407				const char *buf, size_t size)
408{
409	struct qib_diagc_attr *dattr =
410		container_of(attr, struct qib_diagc_attr, attr);
411	struct qib_pportdata *ppd =
412		container_of(kobj, struct qib_pportdata, diagc_kobj);
413	struct qib_ibport *qibp = &ppd->ibport_data;
414	char *endp;
415	long val = simple_strtol(buf, &endp, 0);
416
417	if (val < 0 || endp == buf)
418		return -EINVAL;
 
 
 
 
419
420	*(u32 *)((char *) qibp + dattr->counter) = val;
421	return size;
422}
 
423
424static const struct sysfs_ops qib_diagc_ops = {
425	.show = diagc_attr_show,
426	.store = diagc_attr_store,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427};
428
429static struct kobj_type qib_diagc_ktype = {
430	.release = qib_port_release,
431	.sysfs_ops = &qib_diagc_ops,
432	.default_attrs = diagc_default_attributes
433};
434
435/* End diag_counters */
436
 
 
 
 
 
 
 
 
437/* end of per-port file structures and support code */
438
439/*
440 * Start of per-unit (or driver, in some cases, but replicated
441 * per unit) functions (these get a device *)
442 */
443static ssize_t show_rev(struct device *device, struct device_attribute *attr,
444			char *buf)
445{
446	struct qib_ibdev *dev =
447		container_of(device, struct qib_ibdev, ibdev.dev);
448
449	return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
450}
 
451
452static ssize_t show_hca(struct device *device, struct device_attribute *attr,
453			char *buf)
454{
455	struct qib_ibdev *dev =
456		container_of(device, struct qib_ibdev, ibdev.dev);
457	struct qib_devdata *dd = dd_from_dev(dev);
458	int ret;
459
460	if (!dd->boardname)
461		ret = -EINVAL;
462	else
463		ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
464	return ret;
465}
 
 
466
467static ssize_t show_version(struct device *device,
468			    struct device_attribute *attr, char *buf)
469{
470	/* The string printed here is already newline-terminated. */
471	return scnprintf(buf, PAGE_SIZE, "%s", (char *)ib_qib_version);
472}
 
473
474static ssize_t show_boardversion(struct device *device,
475				 struct device_attribute *attr, char *buf)
476{
477	struct qib_ibdev *dev =
478		container_of(device, struct qib_ibdev, ibdev.dev);
479	struct qib_devdata *dd = dd_from_dev(dev);
480
481	/* The string printed here is already newline-terminated. */
482	return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
483}
 
484
485
486static ssize_t show_localbus_info(struct device *device,
487				  struct device_attribute *attr, char *buf)
488{
489	struct qib_ibdev *dev =
490		container_of(device, struct qib_ibdev, ibdev.dev);
491	struct qib_devdata *dd = dd_from_dev(dev);
492
493	/* The string printed here is already newline-terminated. */
494	return scnprintf(buf, PAGE_SIZE, "%s", dd->lbus_info);
495}
 
496
497
498static ssize_t show_nctxts(struct device *device,
499			   struct device_attribute *attr, char *buf)
500{
501	struct qib_ibdev *dev =
502		container_of(device, struct qib_ibdev, ibdev.dev);
503	struct qib_devdata *dd = dd_from_dev(dev);
504
505	/* Return the number of user ports (contexts) available. */
506	return scnprintf(buf, PAGE_SIZE, "%u\n", dd->cfgctxts -
507		dd->first_user_ctxt);
 
 
 
 
508}
 
509
510static ssize_t show_nfreectxts(struct device *device,
511			   struct device_attribute *attr, char *buf)
512{
513	struct qib_ibdev *dev =
514		container_of(device, struct qib_ibdev, ibdev.dev);
515	struct qib_devdata *dd = dd_from_dev(dev);
516
517	/* Return the number of free user ports (contexts) available. */
518	return scnprintf(buf, PAGE_SIZE, "%u\n", dd->cfgctxts -
519		dd->first_user_ctxt - (u32)qib_stats.sps_ctxts);
520}
 
521
522static ssize_t show_serial(struct device *device,
523			   struct device_attribute *attr, char *buf)
524{
525	struct qib_ibdev *dev =
526		container_of(device, struct qib_ibdev, ibdev.dev);
527	struct qib_devdata *dd = dd_from_dev(dev);
 
 
528
529	buf[sizeof dd->serial] = '\0';
530	memcpy(buf, dd->serial, sizeof dd->serial);
531	strcat(buf, "\n");
532	return strlen(buf);
533}
 
534
535static ssize_t store_chip_reset(struct device *device,
536				struct device_attribute *attr, const char *buf,
537				size_t count)
538{
539	struct qib_ibdev *dev =
540		container_of(device, struct qib_ibdev, ibdev.dev);
541	struct qib_devdata *dd = dd_from_dev(dev);
542	int ret;
543
544	if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
545		ret = -EINVAL;
546		goto bail;
547	}
548
549	ret = qib_reset_device(dd->unit);
550bail:
551	return ret < 0 ? ret : count;
552}
553
554static ssize_t show_logged_errs(struct device *device,
555				struct device_attribute *attr, char *buf)
556{
557	struct qib_ibdev *dev =
558		container_of(device, struct qib_ibdev, ibdev.dev);
559	struct qib_devdata *dd = dd_from_dev(dev);
560	int idx, count;
561
562	/* force consistency with actual EEPROM */
563	if (qib_update_eeprom_log(dd) != 0)
564		return -ENXIO;
565
566	count = 0;
567	for (idx = 0; idx < QIB_EEP_LOG_CNT; ++idx) {
568		count += scnprintf(buf + count, PAGE_SIZE - count, "%d%c",
569				   dd->eep_st_errs[idx],
570				   idx == (QIB_EEP_LOG_CNT - 1) ? '\n' : ' ');
571	}
572
573	return count;
574}
575
576/*
577 * Dump tempsense regs. in decimal, to ease shell-scripts.
578 */
579static ssize_t show_tempsense(struct device *device,
580			      struct device_attribute *attr, char *buf)
581{
582	struct qib_ibdev *dev =
583		container_of(device, struct qib_ibdev, ibdev.dev);
584	struct qib_devdata *dd = dd_from_dev(dev);
585	int ret;
586	int idx;
587	u8 regvals[8];
588
589	ret = -ENXIO;
590	for (idx = 0; idx < 8; ++idx) {
591		if (idx == 6)
 
592			continue;
593		ret = dd->f_tempsense_rd(dd, idx);
594		if (ret < 0)
595			break;
596		regvals[idx] = ret;
597	}
598	if (idx == 8)
599		ret = scnprintf(buf, PAGE_SIZE, "%d %d %02X %02X %d %d\n",
600				*(signed char *)(regvals),
601				*(signed char *)(regvals + 1),
602				regvals[2], regvals[3],
603				*(signed char *)(regvals + 5),
604				*(signed char *)(regvals + 7));
605	return ret;
606}
 
607
608/*
609 * end of per-unit (or driver, in some cases, but replicated
610 * per unit) functions
611 */
612
613/* start of per-unit file structures and support code */
614static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
615static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
616static DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
617static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
618static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL);
619static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL);
620static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
621static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
622static DEVICE_ATTR(logged_errors, S_IRUGO, show_logged_errs, NULL);
623static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL);
624static DEVICE_ATTR(localbus_info, S_IRUGO, show_localbus_info, NULL);
625static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset);
626
627static struct device_attribute *qib_attributes[] = {
628	&dev_attr_hw_rev,
629	&dev_attr_hca_type,
630	&dev_attr_board_id,
631	&dev_attr_version,
632	&dev_attr_nctxts,
633	&dev_attr_nfreectxts,
634	&dev_attr_serial,
635	&dev_attr_boardversion,
636	&dev_attr_logged_errors,
637	&dev_attr_tempsense,
638	&dev_attr_localbus_info,
639	&dev_attr_chip_reset,
640};
641
642int qib_create_port_files(struct ib_device *ibdev, u8 port_num,
643			  struct kobject *kobj)
644{
645	struct qib_pportdata *ppd;
646	struct qib_devdata *dd = dd_from_ibdev(ibdev);
647	int ret;
648
649	if (!port_num || port_num > dd->num_pports) {
650		qib_dev_err(dd, "Skipping infiniband class with "
651			    "invalid port %u\n", port_num);
652		ret = -ENODEV;
653		goto bail;
654	}
655	ppd = &dd->pport[port_num - 1];
656
657	ret = kobject_init_and_add(&ppd->pport_kobj, &qib_port_ktype, kobj,
658				   "linkcontrol");
659	if (ret) {
660		qib_dev_err(dd, "Skipping linkcontrol sysfs info, "
661			    "(err %d) port %u\n", ret, port_num);
662		goto bail;
663	}
664	kobject_uevent(&ppd->pport_kobj, KOBJ_ADD);
665
666	ret = kobject_init_and_add(&ppd->sl2vl_kobj, &qib_sl2vl_ktype, kobj,
667				   "sl2vl");
668	if (ret) {
669		qib_dev_err(dd, "Skipping sl2vl sysfs info, "
670			    "(err %d) port %u\n", ret, port_num);
671		goto bail_sl;
672	}
673	kobject_uevent(&ppd->sl2vl_kobj, KOBJ_ADD);
674
675	ret = kobject_init_and_add(&ppd->diagc_kobj, &qib_diagc_ktype, kobj,
676				   "diag_counters");
677	if (ret) {
678		qib_dev_err(dd, "Skipping diag_counters sysfs info, "
679			    "(err %d) port %u\n", ret, port_num);
680		goto bail_diagc;
681	}
682	kobject_uevent(&ppd->diagc_kobj, KOBJ_ADD);
683
684	return 0;
685
686bail_diagc:
687	kobject_put(&ppd->sl2vl_kobj);
688bail_sl:
689	kobject_put(&ppd->pport_kobj);
690bail:
691	return ret;
692}
693
694/*
695 * Register and create our files in /sys/class/infiniband.
696 */
697int qib_verbs_register_sysfs(struct qib_devdata *dd)
698{
699	struct ib_device *dev = &dd->verbs_dev.ibdev;
700	int i, ret;
701
702	for (i = 0; i < ARRAY_SIZE(qib_attributes); ++i) {
703		ret = device_create_file(&dev->dev, qib_attributes[i]);
704		if (ret)
705			return ret;
706	}
707
708	return 0;
709}
710
711/*
712 * Unregister and remove our files in /sys/class/infiniband.
713 */
714void qib_verbs_unregister_sysfs(struct qib_devdata *dd)
715{
716	struct qib_pportdata *ppd;
717	int i;
718
719	for (i = 0; i < dd->num_pports; i++) {
720		ppd = &dd->pport[i];
721		kobject_put(&ppd->pport_kobj);
722		kobject_put(&ppd->sl2vl_kobj);
723	}
724}