1/*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2013 QLogic Corporation
4 *
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
7
8#include <linux/slab.h>
9#include <linux/interrupt.h>
10
11#include "qlcnic.h"
12#include "qlcnic_hw.h"
13
14#include <linux/swab.h>
15#include <linux/dma-mapping.h>
16#include <net/ip.h>
17#include <linux/ipv6.h>
18#include <linux/inetdevice.h>
19#include <linux/sysfs.h>
20#include <linux/aer.h>
21#include <linux/log2.h>
22#ifdef CONFIG_QLCNIC_HWMON
23#include <linux/hwmon.h>
24#include <linux/hwmon-sysfs.h>
25#endif
26
27#define QLC_STATUS_UNSUPPORTED_CMD	-2
28
29int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
30{
31	return -EOPNOTSUPP;
32}
33
34int qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
35{
36	return -EOPNOTSUPP;
37}
38
39static ssize_t qlcnic_store_bridged_mode(struct device *dev,
40					 struct device_attribute *attr,
41					 const char *buf, size_t len)
42{
43	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
44	unsigned long new;
45	int ret = -EINVAL;
46
47	if (!(adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG))
48		goto err_out;
49
50	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
51		goto err_out;
52
53	if (kstrtoul(buf, 2, &new))
54		goto err_out;
55
56	if (!qlcnic_config_bridged_mode(adapter, !!new))
57		ret = len;
58
59err_out:
60	return ret;
61}
62
63static ssize_t qlcnic_show_bridged_mode(struct device *dev,
64					struct device_attribute *attr,
65					char *buf)
66{
67	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
68	int bridged_mode = 0;
69
70	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
71		bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
72
73	return sprintf(buf, "%d\n", bridged_mode);
74}
75
76static ssize_t qlcnic_store_diag_mode(struct device *dev,
77				      struct device_attribute *attr,
78				      const char *buf, size_t len)
79{
80	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
81	unsigned long new;
82
83	if (kstrtoul(buf, 2, &new))
84		return -EINVAL;
85
86	if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
87		adapter->flags ^= QLCNIC_DIAG_ENABLED;
88
89	return len;
90}
91
92static ssize_t qlcnic_show_diag_mode(struct device *dev,
93				     struct device_attribute *attr, char *buf)
94{
95	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
96	return sprintf(buf, "%d\n", !!(adapter->flags & QLCNIC_DIAG_ENABLED));
97}
98
99static int qlcnic_validate_beacon(struct qlcnic_adapter *adapter, u16 beacon,
100				  u8 *state, u8 *rate)
101{
102	*rate = LSB(beacon);
103	*state = MSB(beacon);
104
105	QLCDB(adapter, DRV, "rate %x state %x\n", *rate, *state);
106
107	if (!*state) {
108		*rate = __QLCNIC_MAX_LED_RATE;
109		return 0;
110	} else if (*state > __QLCNIC_MAX_LED_STATE) {
111		return -EINVAL;
112	}
113
114	if ((!*rate) || (*rate > __QLCNIC_MAX_LED_RATE))
115		return -EINVAL;
116
117	return 0;
118}
119
120static int qlcnic_83xx_store_beacon(struct qlcnic_adapter *adapter,
121				    const char *buf, size_t len)
122{
123	struct qlcnic_hardware_context *ahw = adapter->ahw;
124	unsigned long h_beacon;
125	int err;
126
127	if (test_bit(__QLCNIC_RESETTING, &adapter->state))
128		return -EIO;
129
130	if (kstrtoul(buf, 2, &h_beacon))
131		return -EINVAL;
132
133	qlcnic_get_beacon_state(adapter);
134
135	if (ahw->beacon_state == h_beacon)
136		return len;
137
138	rtnl_lock();
139	if (!ahw->beacon_state) {
140		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
141			rtnl_unlock();
142			return -EBUSY;
143		}
144	}
145
146	if (h_beacon)
147		err = qlcnic_83xx_config_led(adapter, 1, h_beacon);
148	else
149		err = qlcnic_83xx_config_led(adapter, 0, !h_beacon);
150	if (!err)
151		ahw->beacon_state = h_beacon;
152
153	if (!ahw->beacon_state)
154		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
155
156	rtnl_unlock();
157	return len;
158}
159
160static int qlcnic_82xx_store_beacon(struct qlcnic_adapter *adapter,
161				    const char *buf, size_t len)
162{
163	struct qlcnic_hardware_context *ahw = adapter->ahw;
164	int err, drv_sds_rings = adapter->drv_sds_rings;
165	u16 beacon;
166	u8 b_state, b_rate;
167
168	if (len != sizeof(u16))
169		return QL_STATUS_INVALID_PARAM;
170
171	memcpy(&beacon, buf, sizeof(u16));
172	err = qlcnic_validate_beacon(adapter, beacon, &b_state, &b_rate);
173	if (err)
174		return err;
175
176	qlcnic_get_beacon_state(adapter);
177
178	if (ahw->beacon_state == b_state)
179		return len;
180
181	rtnl_lock();
182	if (!ahw->beacon_state) {
183		if (test_and_set_bit(__QLCNIC_LED_ENABLE, &adapter->state)) {
184			rtnl_unlock();
185			return -EBUSY;
186		}
187	}
188
189	if (test_bit(__QLCNIC_RESETTING, &adapter->state)) {
190		err = -EIO;
191		goto out;
192	}
193
194	if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
195		err = qlcnic_diag_alloc_res(adapter->netdev, QLCNIC_LED_TEST);
196		if (err)
197			goto out;
198		set_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state);
199	}
200
201	err = qlcnic_config_led(adapter, b_state, b_rate);
202	if (!err) {
203		err = len;
204		ahw->beacon_state = b_state;
205	}
206
207	if (test_and_clear_bit(__QLCNIC_DIAG_RES_ALLOC, &adapter->state))
208		qlcnic_diag_free_res(adapter->netdev, drv_sds_rings);
209
210out:
211	if (!ahw->beacon_state)
212		clear_bit(__QLCNIC_LED_ENABLE, &adapter->state);
213	rtnl_unlock();
214
215	return err;
216}
217
218static ssize_t qlcnic_store_beacon(struct device *dev,
219				   struct device_attribute *attr,
220				   const char *buf, size_t len)
221{
222	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
223	int err = 0;
224
225	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC) {
226		dev_warn(dev,
227			 "LED test not supported in non privileged mode\n");
228		return -EOPNOTSUPP;
229	}
230
231	if (qlcnic_82xx_check(adapter))
232		err = qlcnic_82xx_store_beacon(adapter, buf, len);
233	else if (qlcnic_83xx_check(adapter))
234		err = qlcnic_83xx_store_beacon(adapter, buf, len);
235	else
236		return -EIO;
237
238	return err;
239}
240
241static ssize_t qlcnic_show_beacon(struct device *dev,
242				  struct device_attribute *attr, char *buf)
243{
244	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
245
246	return sprintf(buf, "%d\n", adapter->ahw->beacon_state);
247}
248
249static int qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
250				     loff_t offset, size_t size)
251{
252	size_t crb_size = 4;
253
254	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
255		return -EIO;
256
257	if (offset < QLCNIC_PCI_CRBSPACE) {
258		if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
259				  QLCNIC_PCI_CAMQM_END))
260			crb_size = 8;
261		else
262			return -EINVAL;
263	}
264
265	if ((size != crb_size) || (offset & (crb_size-1)))
266		return  -EINVAL;
267
268	return 0;
269}
270
271static ssize_t qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
272				     struct bin_attribute *attr, char *buf,
273				     loff_t offset, size_t size)
274{
275	struct device *dev = container_of(kobj, struct device, kobj);
276	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
277	int ret;
278
279	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
280	if (ret != 0)
281		return ret;
282	qlcnic_read_crb(adapter, buf, offset, size);
283	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
284
285	return size;
286}
287
288static ssize_t qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
289				      struct bin_attribute *attr, char *buf,
290				      loff_t offset, size_t size)
291{
292	struct device *dev = container_of(kobj, struct device, kobj);
293	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
294	int ret;
295
296	ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
297	if (ret != 0)
298		return ret;
299
300	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
301	qlcnic_write_crb(adapter, buf, offset, size);
302	return size;
303}
304
305static int qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
306				     loff_t offset, size_t size)
307{
308	if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
309		return -EIO;
310
311	if ((size != 8) || (offset & 0x7))
312		return  -EIO;
313
314	return 0;
315}
316
317static ssize_t qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
318				     struct bin_attribute *attr, char *buf,
319				     loff_t offset, size_t size)
320{
321	struct device *dev = container_of(kobj, struct device, kobj);
322	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
323	u64 data;
324	int ret;
325
326	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
327	if (ret != 0)
328		return ret;
329
330	if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
331		return -EIO;
332
333	memcpy(buf, &data, size);
334	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
335
336	return size;
337}
338
339static ssize_t qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
340				      struct bin_attribute *attr, char *buf,
341				      loff_t offset, size_t size)
342{
343	struct device *dev = container_of(kobj, struct device, kobj);
344	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
345	u64 data;
346	int ret;
347
348	ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
349	if (ret != 0)
350		return ret;
351
352	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
353	memcpy(&data, buf, size);
354
355	if (qlcnic_pci_mem_write_2M(adapter, offset, data))
356		return -EIO;
357
358	return size;
359}
360
361int qlcnic_is_valid_nic_func(struct qlcnic_adapter *adapter, u8 pci_func)
362{
363	int i;
364
365	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
366		if (adapter->npars[i].pci_func == pci_func)
367			return i;
368	}
369
370	dev_err(&adapter->pdev->dev, "%s: Invalid nic function\n", __func__);
371	return -EINVAL;
372}
373
374static int validate_pm_config(struct qlcnic_adapter *adapter,
375			      struct qlcnic_pm_func_cfg *pm_cfg, int count)
376{
377	u8 src_pci_func, s_esw_id, d_esw_id;
378	u8 dest_pci_func;
379	int i, src_index, dest_index;
380
381	for (i = 0; i < count; i++) {
382		src_pci_func = pm_cfg[i].pci_func;
383		dest_pci_func = pm_cfg[i].dest_npar;
384		src_index = qlcnic_is_valid_nic_func(adapter, src_pci_func);
385		if (src_index < 0)
386			return QL_STATUS_INVALID_PARAM;
387
388		dest_index = qlcnic_is_valid_nic_func(adapter, dest_pci_func);
389		if (dest_index < 0)
390			return QL_STATUS_INVALID_PARAM;
391
392		s_esw_id = adapter->npars[src_index].phy_port;
393		d_esw_id = adapter->npars[dest_index].phy_port;
394
395		if (s_esw_id != d_esw_id)
396			return QL_STATUS_INVALID_PARAM;
397	}
398
399	return 0;
400}
401
402static ssize_t qlcnic_sysfs_write_pm_config(struct file *filp,
403					    struct kobject *kobj,
404					    struct bin_attribute *attr,
405					    char *buf, loff_t offset,
406					    size_t size)
407{
408	struct device *dev = container_of(kobj, struct device, kobj);
409	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
410	struct qlcnic_pm_func_cfg *pm_cfg;
411	u32 id, action, pci_func;
412	int count, rem, i, ret, index;
413
414	count	= size / sizeof(struct qlcnic_pm_func_cfg);
415	rem	= size % sizeof(struct qlcnic_pm_func_cfg);
416	if (rem)
417		return QL_STATUS_INVALID_PARAM;
418
419	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
420	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
421	ret = validate_pm_config(adapter, pm_cfg, count);
422
423	if (ret)
424		return ret;
425	for (i = 0; i < count; i++) {
426		pci_func = pm_cfg[i].pci_func;
427		action = !!pm_cfg[i].action;
428		index = qlcnic_is_valid_nic_func(adapter, pci_func);
429		if (index < 0)
430			return QL_STATUS_INVALID_PARAM;
431
432		id = adapter->npars[index].phy_port;
433		ret = qlcnic_config_port_mirroring(adapter, id,
434						   action, pci_func);
435		if (ret)
436			return ret;
437	}
438
439	for (i = 0; i < count; i++) {
440		pci_func = pm_cfg[i].pci_func;
441		index = qlcnic_is_valid_nic_func(adapter, pci_func);
442		if (index < 0)
443			return QL_STATUS_INVALID_PARAM;
444		id = adapter->npars[index].phy_port;
445		adapter->npars[index].enable_pm = !!pm_cfg[i].action;
446		adapter->npars[index].dest_npar = id;
447	}
448
449	return size;
450}
451
452static ssize_t qlcnic_sysfs_read_pm_config(struct file *filp,
453					   struct kobject *kobj,
454					   struct bin_attribute *attr,
455					   char *buf, loff_t offset,
456					   size_t size)
457{
458	struct device *dev = container_of(kobj, struct device, kobj);
459	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
460	struct qlcnic_pm_func_cfg *pm_cfg;
461	u8 pci_func;
462	u32 count;
463	int i;
464
465	memset(buf, 0, size);
466	pm_cfg = (struct qlcnic_pm_func_cfg *)buf;
467	count = size / sizeof(struct qlcnic_pm_func_cfg);
468	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
469		pci_func = adapter->npars[i].pci_func;
470		if (pci_func >= count) {
471			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
472				__func__, adapter->ahw->total_nic_func, count);
473			continue;
474		}
475		if (!adapter->npars[i].eswitch_status)
476			continue;
477
478		pm_cfg[pci_func].action = adapter->npars[i].enable_pm;
479		pm_cfg[pci_func].dest_npar = 0;
480		pm_cfg[pci_func].pci_func = i;
481	}
482	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
483	return size;
484}
485
486static int validate_esw_config(struct qlcnic_adapter *adapter,
487			       struct qlcnic_esw_func_cfg *esw_cfg, int count)
488{
489	struct qlcnic_hardware_context *ahw = adapter->ahw;
490	int i, ret;
491	u32 op_mode;
492	u8 pci_func;
493
494	if (qlcnic_82xx_check(adapter))
495		op_mode = readl(ahw->pci_base0 + QLCNIC_DRV_OP_MODE);
496	else
497		op_mode = QLCRDX(ahw, QLC_83XX_DRV_OP_MODE);
498
499	for (i = 0; i < count; i++) {
500		pci_func = esw_cfg[i].pci_func;
501		if (pci_func >= ahw->max_vnic_func)
502			return QL_STATUS_INVALID_PARAM;
503
504		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
505			if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
506				return QL_STATUS_INVALID_PARAM;
507
508		switch (esw_cfg[i].op_mode) {
509		case QLCNIC_PORT_DEFAULTS:
510			if (qlcnic_82xx_check(adapter)) {
511				ret = QLC_DEV_GET_DRV(op_mode, pci_func);
512			} else {
513				ret = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
514								  pci_func);
515				esw_cfg[i].offload_flags = 0;
516			}
517
518			if (ret != QLCNIC_NON_PRIV_FUNC) {
519				if (esw_cfg[i].mac_anti_spoof != 0)
520					return QL_STATUS_INVALID_PARAM;
521				if (esw_cfg[i].mac_override != 1)
522					return QL_STATUS_INVALID_PARAM;
523				if (esw_cfg[i].promisc_mode != 1)
524					return QL_STATUS_INVALID_PARAM;
525			}
526			break;
527		case QLCNIC_ADD_VLAN:
528			if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
529				return QL_STATUS_INVALID_PARAM;
530			if (!esw_cfg[i].op_type)
531				return QL_STATUS_INVALID_PARAM;
532			break;
533		case QLCNIC_DEL_VLAN:
534			if (!esw_cfg[i].op_type)
535				return QL_STATUS_INVALID_PARAM;
536			break;
537		default:
538			return QL_STATUS_INVALID_PARAM;
539		}
540	}
541
542	return 0;
543}
544
545static ssize_t qlcnic_sysfs_write_esw_config(struct file *file,
546					     struct kobject *kobj,
547					     struct bin_attribute *attr,
548					     char *buf, loff_t offset,
549					     size_t size)
550{
551	struct device *dev = container_of(kobj, struct device, kobj);
552	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
553	struct qlcnic_esw_func_cfg *esw_cfg;
554	struct qlcnic_npar_info *npar;
555	int count, rem, i, ret;
556	int index;
557	u8 op_mode = 0, pci_func;
558
559	count	= size / sizeof(struct qlcnic_esw_func_cfg);
560	rem	= size % sizeof(struct qlcnic_esw_func_cfg);
561	if (rem)
562		return QL_STATUS_INVALID_PARAM;
563
564	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
565	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
566	ret = validate_esw_config(adapter, esw_cfg, count);
567	if (ret)
568		return ret;
569
570	for (i = 0; i < count; i++) {
571		if (adapter->ahw->op_mode == QLCNIC_MGMT_FUNC)
572			if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
573				return QL_STATUS_INVALID_PARAM;
574
575		if (adapter->ahw->pci_func != esw_cfg[i].pci_func)
576			continue;
577
578		op_mode = esw_cfg[i].op_mode;
579		qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
580		esw_cfg[i].op_mode = op_mode;
581		esw_cfg[i].pci_func = adapter->ahw->pci_func;
582
583		switch (esw_cfg[i].op_mode) {
584		case QLCNIC_PORT_DEFAULTS:
585			qlcnic_set_eswitch_port_features(adapter, &esw_cfg[i]);
586			rtnl_lock();
587			qlcnic_set_netdev_features(adapter, &esw_cfg[i]);
588			rtnl_unlock();
589			break;
590		case QLCNIC_ADD_VLAN:
591			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
592			break;
593		case QLCNIC_DEL_VLAN:
594			esw_cfg[i].vlan_id = 0;
595			qlcnic_set_vlan_config(adapter, &esw_cfg[i]);
596			break;
597		}
598	}
599
600	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
601		goto out;
602
603	for (i = 0; i < count; i++) {
604		pci_func = esw_cfg[i].pci_func;
605		index = qlcnic_is_valid_nic_func(adapter, pci_func);
606		if (index < 0)
607			return QL_STATUS_INVALID_PARAM;
608		npar = &adapter->npars[index];
609		switch (esw_cfg[i].op_mode) {
610		case QLCNIC_PORT_DEFAULTS:
611			npar->promisc_mode = esw_cfg[i].promisc_mode;
612			npar->mac_override = esw_cfg[i].mac_override;
613			npar->offload_flags = esw_cfg[i].offload_flags;
614			npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
615			npar->discard_tagged = esw_cfg[i].discard_tagged;
616			break;
617		case QLCNIC_ADD_VLAN:
618			npar->pvid = esw_cfg[i].vlan_id;
619			break;
620		case QLCNIC_DEL_VLAN:
621			npar->pvid = 0;
622			break;
623		}
624	}
625out:
626	return size;
627}
628
629static ssize_t qlcnic_sysfs_read_esw_config(struct file *file,
630					    struct kobject *kobj,
631					    struct bin_attribute *attr,
632					    char *buf, loff_t offset,
633					    size_t size)
634{
635	struct device *dev = container_of(kobj, struct device, kobj);
636	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
637	struct qlcnic_esw_func_cfg *esw_cfg;
638	u8 pci_func;
639	u32 count;
640	int i;
641
642	memset(buf, 0, size);
643	esw_cfg = (struct qlcnic_esw_func_cfg *)buf;
644	count = size / sizeof(struct qlcnic_esw_func_cfg);
645	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
646		pci_func = adapter->npars[i].pci_func;
647		if (pci_func >= count) {
648			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
649				__func__, adapter->ahw->total_nic_func, count);
650			continue;
651		}
652		if (!adapter->npars[i].eswitch_status)
653			continue;
654
655		esw_cfg[pci_func].pci_func = pci_func;
656		if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[pci_func]))
657			return QL_STATUS_INVALID_PARAM;
658	}
659	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
660	return size;
661}
662
663static int validate_npar_config(struct qlcnic_adapter *adapter,
664				struct qlcnic_npar_func_cfg *np_cfg,
665				int count)
666{
667	u8 pci_func, i;
668
669	for (i = 0; i < count; i++) {
670		pci_func = np_cfg[i].pci_func;
671		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
672			return QL_STATUS_INVALID_PARAM;
673
674		if (!IS_VALID_BW(np_cfg[i].min_bw) ||
675		    !IS_VALID_BW(np_cfg[i].max_bw))
676			return QL_STATUS_INVALID_PARAM;
677	}
678	return 0;
679}
680
681static ssize_t qlcnic_sysfs_write_npar_config(struct file *file,
682					      struct kobject *kobj,
683					      struct bin_attribute *attr,
684					      char *buf, loff_t offset,
685					      size_t size)
686{
687	struct device *dev = container_of(kobj, struct device, kobj);
688	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
689	struct qlcnic_info nic_info;
690	struct qlcnic_npar_func_cfg *np_cfg;
691	int i, count, rem, ret, index;
692	u8 pci_func;
693
694	count	= size / sizeof(struct qlcnic_npar_func_cfg);
695	rem	= size % sizeof(struct qlcnic_npar_func_cfg);
696	if (rem)
697		return QL_STATUS_INVALID_PARAM;
698
699	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
700	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
701	ret = validate_npar_config(adapter, np_cfg, count);
702	if (ret)
703		return ret;
704
705	for (i = 0; i < count; i++) {
706		pci_func = np_cfg[i].pci_func;
707
708		memset(&nic_info, 0, sizeof(struct qlcnic_info));
709		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
710		if (ret)
711			return ret;
712		nic_info.pci_func = pci_func;
713		nic_info.min_tx_bw = np_cfg[i].min_bw;
714		nic_info.max_tx_bw = np_cfg[i].max_bw;
715		ret = qlcnic_set_nic_info(adapter, &nic_info);
716		if (ret)
717			return ret;
718		index = qlcnic_is_valid_nic_func(adapter, pci_func);
719		if (index < 0)
720			return QL_STATUS_INVALID_PARAM;
721		adapter->npars[index].min_bw = nic_info.min_tx_bw;
722		adapter->npars[index].max_bw = nic_info.max_tx_bw;
723	}
724
725	return size;
726}
727
728static ssize_t qlcnic_sysfs_read_npar_config(struct file *file,
729					     struct kobject *kobj,
730					     struct bin_attribute *attr,
731					     char *buf, loff_t offset,
732					     size_t size)
733{
734	struct device *dev = container_of(kobj, struct device, kobj);
735	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
736	struct qlcnic_npar_func_cfg *np_cfg;
737	struct qlcnic_info nic_info;
738	u8 pci_func;
739	int i, ret;
740	u32 count;
741
742	memset(&nic_info, 0, sizeof(struct qlcnic_info));
743	memset(buf, 0, size);
744	np_cfg = (struct qlcnic_npar_func_cfg *)buf;
745
746	count = size / sizeof(struct qlcnic_npar_func_cfg);
747	for (i = 0; i < adapter->ahw->total_nic_func; i++) {
748		if (adapter->npars[i].pci_func >= count) {
749			dev_dbg(dev, "%s: Total nic functions[%d], App sent function count[%d]\n",
750				__func__, adapter->ahw->total_nic_func, count);
751			continue;
752		}
753		if (!adapter->npars[i].eswitch_status)
754			continue;
755		pci_func = adapter->npars[i].pci_func;
756		if (qlcnic_is_valid_nic_func(adapter, pci_func) < 0)
757			continue;
758		ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
759		if (ret)
760			return ret;
761
762		np_cfg[pci_func].pci_func = pci_func;
763		np_cfg[pci_func].op_mode = (u8)nic_info.op_mode;
764		np_cfg[pci_func].port_num = nic_info.phys_port;
765		np_cfg[pci_func].fw_capab = nic_info.capabilities;
766		np_cfg[pci_func].min_bw = nic_info.min_tx_bw;
767		np_cfg[pci_func].max_bw = nic_info.max_tx_bw;
768		np_cfg[pci_func].max_tx_queues = nic_info.max_tx_ques;
769		np_cfg[pci_func].max_rx_queues = nic_info.max_rx_ques;
770	}
771	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
772	return size;
773}
774
775static ssize_t qlcnic_sysfs_get_port_stats(struct file *file,
776					   struct kobject *kobj,
777					   struct bin_attribute *attr,
778					   char *buf, loff_t offset,
779					   size_t size)
780{
781	struct device *dev = container_of(kobj, struct device, kobj);
782	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
783	struct qlcnic_esw_statistics port_stats;
784	int ret;
785
786	if (qlcnic_83xx_check(adapter))
787		return QLC_STATUS_UNSUPPORTED_CMD;
788
789	if (size != sizeof(struct qlcnic_esw_statistics))
790		return QL_STATUS_INVALID_PARAM;
791
792	if (offset >= adapter->ahw->max_vnic_func)
793		return QL_STATUS_INVALID_PARAM;
794
795	memset(&port_stats, 0, size);
796	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
797				    &port_stats.rx);
798	if (ret)
799		return ret;
800
801	ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
802				    &port_stats.tx);
803	if (ret)
804		return ret;
805
806	memcpy(buf, &port_stats, size);
807	return size;
808}
809
810static ssize_t qlcnic_sysfs_get_esw_stats(struct file *file,
811					  struct kobject *kobj,
812					  struct bin_attribute *attr,
813					  char *buf, loff_t offset,
814					  size_t size)
815{
816	struct device *dev = container_of(kobj, struct device, kobj);
817	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
818	struct qlcnic_esw_statistics esw_stats;
819	int ret;
820
821	if (qlcnic_83xx_check(adapter))
822		return QLC_STATUS_UNSUPPORTED_CMD;
823
824	if (size != sizeof(struct qlcnic_esw_statistics))
825		return QL_STATUS_INVALID_PARAM;
826
827	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
828		return QL_STATUS_INVALID_PARAM;
829
830	memset(&esw_stats, 0, size);
831	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
832				       &esw_stats.rx);
833	if (ret)
834		return ret;
835
836	ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
837				       &esw_stats.tx);
838	if (ret)
839		return ret;
840
841	memcpy(buf, &esw_stats, size);
842	return size;
843}
844
845static ssize_t qlcnic_sysfs_clear_esw_stats(struct file *file,
846					    struct kobject *kobj,
847					    struct bin_attribute *attr,
848					    char *buf, loff_t offset,
849					    size_t size)
850{
851	struct device *dev = container_of(kobj, struct device, kobj);
852	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
853	int ret;
854
855	if (qlcnic_83xx_check(adapter))
856		return QLC_STATUS_UNSUPPORTED_CMD;
857
858	if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
859		return QL_STATUS_INVALID_PARAM;
860
861	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
862				     QLCNIC_QUERY_RX_COUNTER);
863	if (ret)
864		return ret;
865
866	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
867				     QLCNIC_QUERY_TX_COUNTER);
868	if (ret)
869		return ret;
870
871	return size;
872}
873
874static ssize_t qlcnic_sysfs_clear_port_stats(struct file *file,
875					     struct kobject *kobj,
876					     struct bin_attribute *attr,
877					     char *buf, loff_t offset,
878					     size_t size)
879{
880
881	struct device *dev = container_of(kobj, struct device, kobj);
882	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
883	int ret;
884
885	if (qlcnic_83xx_check(adapter))
886		return QLC_STATUS_UNSUPPORTED_CMD;
887
888	if (offset >= adapter->ahw->max_vnic_func)
889		return QL_STATUS_INVALID_PARAM;
890
891	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
892				     QLCNIC_QUERY_RX_COUNTER);
893	if (ret)
894		return ret;
895
896	ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
897				     QLCNIC_QUERY_TX_COUNTER);
898	if (ret)
899		return ret;
900
901	return size;
902}
903
904static ssize_t qlcnic_sysfs_read_pci_config(struct file *file,
905					    struct kobject *kobj,
906					    struct bin_attribute *attr,
907					    char *buf, loff_t offset,
908					    size_t size)
909{
910	struct device *dev = container_of(kobj, struct device, kobj);
911	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
912	struct qlcnic_pci_func_cfg *pci_cfg;
913	struct qlcnic_pci_info *pci_info;
914	int i, ret;
915	u32 count;
916
917	pci_info = kcalloc(size, sizeof(*pci_info), GFP_KERNEL);
918	if (!pci_info)
919		return -ENOMEM;
920
921	ret = qlcnic_get_pci_info(adapter, pci_info);
922	if (ret) {
923		kfree(pci_info);
924		return ret;
925	}
926
927	pci_cfg = (struct qlcnic_pci_func_cfg *)buf;
928	count = size / sizeof(struct qlcnic_pci_func_cfg);
929	qlcnic_swap32_buffer((u32 *)pci_info, size / sizeof(u32));
930	for (i = 0; i < count; i++) {
931		pci_cfg[i].pci_func = pci_info[i].id;
932		pci_cfg[i].func_type = pci_info[i].type;
933		pci_cfg[i].func_state = 0;
934		pci_cfg[i].port_num = pci_info[i].default_port;
935		pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
936		pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
937		memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
938	}
939
940	kfree(pci_info);
941	return size;
942}
943
944static ssize_t qlcnic_83xx_sysfs_flash_read_handler(struct file *filp,
945						    struct kobject *kobj,
946						    struct bin_attribute *attr,
947						    char *buf, loff_t offset,
948						    size_t size)
949{
950	unsigned char *p_read_buf;
951	int  ret, count;
952	struct device *dev = container_of(kobj, struct device, kobj);
953	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
954
955	if (!size)
956		return QL_STATUS_INVALID_PARAM;
957	if (!buf)
958		return QL_STATUS_INVALID_PARAM;
959
960	count = size / sizeof(u32);
961
962	if (size % sizeof(u32))
963		count++;
964
965	p_read_buf = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
966	if (!p_read_buf)
967		return -ENOMEM;
968	if (qlcnic_83xx_lock_flash(adapter) != 0) {
969		kfree(p_read_buf);
970		return -EIO;
971	}
972
973	ret = qlcnic_83xx_lockless_flash_read32(adapter, offset, p_read_buf,
974						count);
975
976	if (ret) {
977		qlcnic_83xx_unlock_flash(adapter);
978		kfree(p_read_buf);
979		return ret;
980	}
981
982	qlcnic_83xx_unlock_flash(adapter);
983	qlcnic_swap32_buffer((u32 *)p_read_buf, count);
984	memcpy(buf, p_read_buf, size);
985	kfree(p_read_buf);
986
987	return size;
988}
989
990static int qlcnic_83xx_sysfs_flash_bulk_write(struct qlcnic_adapter *adapter,
991					      char *buf, loff_t offset,
992					      size_t size)
993{
994	int  i, ret, count;
995	unsigned char *p_cache, *p_src;
996
997	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
998	if (!p_cache)
999		return -ENOMEM;
1000
1001	count = size / sizeof(u32);
1002	qlcnic_swap32_buffer((u32 *)buf, count);
1003	memcpy(p_cache, buf, size);
1004	p_src = p_cache;
1005
1006	if (qlcnic_83xx_lock_flash(adapter) != 0) {
1007		kfree(p_cache);
1008		return -EIO;
1009	}
1010
1011	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1012		ret = qlcnic_83xx_enable_flash_write(adapter);
1013		if (ret) {
1014			kfree(p_cache);
1015			qlcnic_83xx_unlock_flash(adapter);
1016			return -EIO;
1017		}
1018	}
1019
1020	for (i = 0; i < count / QLC_83XX_FLASH_WRITE_MAX; i++) {
1021		ret = qlcnic_83xx_flash_bulk_write(adapter, offset,
1022						   (u32 *)p_src,
1023						   QLC_83XX_FLASH_WRITE_MAX);
1024
1025		if (ret) {
1026			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1027				ret = qlcnic_83xx_disable_flash_write(adapter);
1028				if (ret) {
1029					kfree(p_cache);
1030					qlcnic_83xx_unlock_flash(adapter);
1031					return -EIO;
1032				}
1033			}
1034
1035			kfree(p_cache);
1036			qlcnic_83xx_unlock_flash(adapter);
1037			return -EIO;
1038		}
1039
1040		p_src = p_src + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1041		offset = offset + sizeof(u32)*QLC_83XX_FLASH_WRITE_MAX;
1042	}
1043
1044	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1045		ret = qlcnic_83xx_disable_flash_write(adapter);
1046		if (ret) {
1047			kfree(p_cache);
1048			qlcnic_83xx_unlock_flash(adapter);
1049			return -EIO;
1050		}
1051	}
1052
1053	kfree(p_cache);
1054	qlcnic_83xx_unlock_flash(adapter);
1055
1056	return 0;
1057}
1058
1059static int qlcnic_83xx_sysfs_flash_write(struct qlcnic_adapter *adapter,
1060					 char *buf, loff_t offset, size_t size)
1061{
1062	int  i, ret, count;
1063	unsigned char *p_cache, *p_src;
1064
1065	p_cache = kcalloc(size, sizeof(unsigned char), GFP_KERNEL);
1066	if (!p_cache)
1067		return -ENOMEM;
1068
1069	qlcnic_swap32_buffer((u32 *)buf, size / sizeof(u32));
1070	memcpy(p_cache, buf, size);
1071	p_src = p_cache;
1072	count = size / sizeof(u32);
1073
1074	if (qlcnic_83xx_lock_flash(adapter) != 0) {
1075		kfree(p_cache);
1076		return -EIO;
1077	}
1078
1079	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1080		ret = qlcnic_83xx_enable_flash_write(adapter);
1081		if (ret) {
1082			kfree(p_cache);
1083			qlcnic_83xx_unlock_flash(adapter);
1084			return -EIO;
1085		}
1086	}
1087
1088	for (i = 0; i < count; i++) {
1089		ret = qlcnic_83xx_flash_write32(adapter, offset, (u32 *)p_src);
1090		if (ret) {
1091			if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1092				ret = qlcnic_83xx_disable_flash_write(adapter);
1093				if (ret) {
1094					kfree(p_cache);
1095					qlcnic_83xx_unlock_flash(adapter);
1096					return -EIO;
1097				}
1098			}
1099			kfree(p_cache);
1100			qlcnic_83xx_unlock_flash(adapter);
1101			return -EIO;
1102		}
1103
1104		p_src = p_src + sizeof(u32);
1105		offset = offset + sizeof(u32);
1106	}
1107
1108	if (adapter->ahw->fdt.mfg_id == adapter->flash_mfg_id) {
1109		ret = qlcnic_83xx_disable_flash_write(adapter);
1110		if (ret) {
1111			kfree(p_cache);
1112			qlcnic_83xx_unlock_flash(adapter);
1113			return -EIO;
1114		}
1115	}
1116
1117	kfree(p_cache);
1118	qlcnic_83xx_unlock_flash(adapter);
1119
1120	return 0;
1121}
1122
1123static ssize_t qlcnic_83xx_sysfs_flash_write_handler(struct file *filp,
1124						     struct kobject *kobj,
1125						     struct bin_attribute *attr,
1126						     char *buf, loff_t offset,
1127						     size_t size)
1128{
1129	int  ret;
1130	static int flash_mode;
1131	unsigned long data;
1132	struct device *dev = container_of(kobj, struct device, kobj);
1133	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1134
1135	if (!buf)
1136		return QL_STATUS_INVALID_PARAM;
1137
1138	ret = kstrtoul(buf, 16, &data);
1139
1140	switch (data) {
1141	case QLC_83XX_FLASH_SECTOR_ERASE_CMD:
1142		flash_mode = QLC_83XX_ERASE_MODE;
1143		ret = qlcnic_83xx_erase_flash_sector(adapter, offset);
1144		if (ret) {
1145			dev_err(&adapter->pdev->dev,
1146				"%s failed at %d\n", __func__, __LINE__);
1147			return -EIO;
1148		}
1149		break;
1150
1151	case QLC_83XX_FLASH_BULK_WRITE_CMD:
1152		flash_mode = QLC_83XX_BULK_WRITE_MODE;
1153		break;
1154
1155	case QLC_83XX_FLASH_WRITE_CMD:
1156		flash_mode = QLC_83XX_WRITE_MODE;
1157		break;
1158	default:
1159		if (flash_mode == QLC_83XX_BULK_WRITE_MODE) {
1160			ret = qlcnic_83xx_sysfs_flash_bulk_write(adapter, buf,
1161								 offset, size);
1162			if (ret) {
1163				dev_err(&adapter->pdev->dev,
1164					"%s failed at %d\n",
1165					__func__, __LINE__);
1166				return -EIO;
1167			}
1168		}
1169
1170		if (flash_mode == QLC_83XX_WRITE_MODE) {
1171			ret = qlcnic_83xx_sysfs_flash_write(adapter, buf,
1172							    offset, size);
1173			if (ret) {
1174				dev_err(&adapter->pdev->dev,
1175					"%s failed at %d\n", __func__,
1176					__LINE__);
1177				return -EIO;
1178			}
1179		}
1180	}
1181
1182	return size;
1183}
1184
1185static struct device_attribute dev_attr_bridged_mode = {
1186       .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
1187       .show = qlcnic_show_bridged_mode,
1188       .store = qlcnic_store_bridged_mode,
1189};
1190
1191static struct device_attribute dev_attr_diag_mode = {
1192	.attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
1193	.show = qlcnic_show_diag_mode,
1194	.store = qlcnic_store_diag_mode,
1195};
1196
1197static struct device_attribute dev_attr_beacon = {
1198	.attr = {.name = "beacon", .mode = (S_IRUGO | S_IWUSR)},
1199	.show = qlcnic_show_beacon,
1200	.store = qlcnic_store_beacon,
1201};
1202
1203static struct bin_attribute bin_attr_crb = {
1204	.attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
1205	.size = 0,
1206	.read = qlcnic_sysfs_read_crb,
1207	.write = qlcnic_sysfs_write_crb,
1208};
1209
1210static struct bin_attribute bin_attr_mem = {
1211	.attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
1212	.size = 0,
1213	.read = qlcnic_sysfs_read_mem,
1214	.write = qlcnic_sysfs_write_mem,
1215};
1216
1217static struct bin_attribute bin_attr_npar_config = {
1218	.attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
1219	.size = 0,
1220	.read = qlcnic_sysfs_read_npar_config,
1221	.write = qlcnic_sysfs_write_npar_config,
1222};
1223
1224static struct bin_attribute bin_attr_pci_config = {
1225	.attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
1226	.size = 0,
1227	.read = qlcnic_sysfs_read_pci_config,
1228	.write = NULL,
1229};
1230
1231static struct bin_attribute bin_attr_port_stats = {
1232	.attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
1233	.size = 0,
1234	.read = qlcnic_sysfs_get_port_stats,
1235	.write = qlcnic_sysfs_clear_port_stats,
1236};
1237
1238static struct bin_attribute bin_attr_esw_stats = {
1239	.attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
1240	.size = 0,
1241	.read = qlcnic_sysfs_get_esw_stats,
1242	.write = qlcnic_sysfs_clear_esw_stats,
1243};
1244
1245static struct bin_attribute bin_attr_esw_config = {
1246	.attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
1247	.size = 0,
1248	.read = qlcnic_sysfs_read_esw_config,
1249	.write = qlcnic_sysfs_write_esw_config,
1250};
1251
1252static struct bin_attribute bin_attr_pm_config = {
1253	.attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
1254	.size = 0,
1255	.read = qlcnic_sysfs_read_pm_config,
1256	.write = qlcnic_sysfs_write_pm_config,
1257};
1258
1259static struct bin_attribute bin_attr_flash = {
1260	.attr = {.name = "flash", .mode = (S_IRUGO | S_IWUSR)},
1261	.size = 0,
1262	.read = qlcnic_83xx_sysfs_flash_read_handler,
1263	.write = qlcnic_83xx_sysfs_flash_write_handler,
1264};
1265
1266#ifdef CONFIG_QLCNIC_HWMON
1267
1268static ssize_t qlcnic_hwmon_show_temp(struct device *dev,
1269				      struct device_attribute *dev_attr,
1270				      char *buf)
1271{
1272	struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
1273	unsigned int temperature = 0, value = 0;
1274
1275	if (qlcnic_83xx_check(adapter))
1276		value = QLCRDX(adapter->ahw, QLC_83XX_ASIC_TEMP);
1277	else if (qlcnic_82xx_check(adapter))
1278		value = QLC_SHARED_REG_RD32(adapter, QLCNIC_ASIC_TEMP);
1279
1280	temperature = qlcnic_get_temp_val(value);
1281	/* display millidegree celcius */
1282	temperature *= 1000;
1283	return sprintf(buf, "%u\n", temperature);
1284}
1285
1286/* hwmon-sysfs attributes */
1287static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1288			  qlcnic_hwmon_show_temp, NULL, 1);
1289
1290static struct attribute *qlcnic_hwmon_attrs[] = {
1291	&sensor_dev_attr_temp1_input.dev_attr.attr,
1292	NULL
1293};
1294
1295ATTRIBUTE_GROUPS(qlcnic_hwmon);
1296
1297void qlcnic_register_hwmon_dev(struct qlcnic_adapter *adapter)
1298{
1299	struct device *dev = &adapter->pdev->dev;
1300	struct device *hwmon_dev;
1301
1302	/* Skip hwmon registration for a VF device */
1303	if (qlcnic_sriov_vf_check(adapter)) {
1304		adapter->ahw->hwmon_dev = NULL;
1305		return;
1306	}
1307	hwmon_dev = hwmon_device_register_with_groups(dev, qlcnic_driver_name,
1308						      adapter,
1309						      qlcnic_hwmon_groups);
1310	if (IS_ERR(hwmon_dev)) {
1311		dev_err(dev, "Cannot register with hwmon, err=%ld\n",
1312			PTR_ERR(hwmon_dev));
1313		hwmon_dev = NULL;
1314	}
1315	adapter->ahw->hwmon_dev = hwmon_dev;
1316}
1317
1318void qlcnic_unregister_hwmon_dev(struct qlcnic_adapter *adapter)
1319{
1320	struct device *hwmon_dev = adapter->ahw->hwmon_dev;
1321	if (hwmon_dev) {
1322		hwmon_device_unregister(hwmon_dev);
1323		adapter->ahw->hwmon_dev = NULL;
1324	}
1325}
1326#endif
1327
1328void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
1329{
1330	struct device *dev = &adapter->pdev->dev;
1331
1332	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1333		if (device_create_file(dev, &dev_attr_bridged_mode))
1334			dev_warn(dev,
1335				 "failed to create bridged_mode sysfs entry\n");
1336}
1337
1338void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
1339{
1340	struct device *dev = &adapter->pdev->dev;
1341
1342	if (adapter->ahw->capabilities & QLCNIC_FW_CAPABILITY_BDG)
1343		device_remove_file(dev, &dev_attr_bridged_mode);
1344}
1345
1346static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
1347{
1348	struct device *dev = &adapter->pdev->dev;
1349
1350	if (device_create_bin_file(dev, &bin_attr_port_stats))
1351		dev_info(dev, "failed to create port stats sysfs entry");
1352
1353	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1354		return;
1355	if (device_create_file(dev, &dev_attr_diag_mode))
1356		dev_info(dev, "failed to create diag_mode sysfs entry\n");
1357	if (device_create_bin_file(dev, &bin_attr_crb))
1358		dev_info(dev, "failed to create crb sysfs entry\n");
1359	if (device_create_bin_file(dev, &bin_attr_mem))
1360		dev_info(dev, "failed to create mem sysfs entry\n");
1361
1362	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1363		return;
1364
1365	if (device_create_bin_file(dev, &bin_attr_pci_config))
1366		dev_info(dev, "failed to create pci config sysfs entry");
1367
1368	if (device_create_file(dev, &dev_attr_beacon))
1369		dev_info(dev, "failed to create beacon sysfs entry");
1370
1371	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1372		return;
1373	if (device_create_bin_file(dev, &bin_attr_esw_config))
1374		dev_info(dev, "failed to create esw config sysfs entry");
1375	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1376		return;
1377	if (device_create_bin_file(dev, &bin_attr_npar_config))
1378		dev_info(dev, "failed to create npar config sysfs entry");
1379	if (device_create_bin_file(dev, &bin_attr_pm_config))
1380		dev_info(dev, "failed to create pm config sysfs entry");
1381	if (device_create_bin_file(dev, &bin_attr_esw_stats))
1382		dev_info(dev, "failed to create eswitch stats sysfs entry");
1383}
1384
1385static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
1386{
1387	struct device *dev = &adapter->pdev->dev;
1388
1389	device_remove_bin_file(dev, &bin_attr_port_stats);
1390
1391	if (adapter->ahw->op_mode == QLCNIC_NON_PRIV_FUNC)
1392		return;
1393	device_remove_file(dev, &dev_attr_diag_mode);
1394	device_remove_bin_file(dev, &bin_attr_crb);
1395	device_remove_bin_file(dev, &bin_attr_mem);
1396
1397	if (test_bit(__QLCNIC_MAINTENANCE_MODE, &adapter->state))
1398		return;
1399
1400	device_remove_bin_file(dev, &bin_attr_pci_config);
1401	device_remove_file(dev, &dev_attr_beacon);
1402	if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
1403		return;
1404	device_remove_bin_file(dev, &bin_attr_esw_config);
1405	if (adapter->ahw->op_mode != QLCNIC_MGMT_FUNC)
1406		return;
1407	device_remove_bin_file(dev, &bin_attr_npar_config);
1408	device_remove_bin_file(dev, &bin_attr_pm_config);
1409	device_remove_bin_file(dev, &bin_attr_esw_stats);
1410}
1411
1412void qlcnic_82xx_add_sysfs(struct qlcnic_adapter *adapter)
1413{
1414	qlcnic_create_diag_entries(adapter);
1415}
1416
1417void qlcnic_82xx_remove_sysfs(struct qlcnic_adapter *adapter)
1418{
1419	qlcnic_remove_diag_entries(adapter);
1420}
1421
1422void qlcnic_83xx_add_sysfs(struct qlcnic_adapter *adapter)
1423{
1424	struct device *dev = &adapter->pdev->dev;
1425
1426	qlcnic_create_diag_entries(adapter);
1427
1428	if (sysfs_create_bin_file(&dev->kobj, &bin_attr_flash))
1429		dev_info(dev, "failed to create flash sysfs entry\n");
1430}
1431
1432void qlcnic_83xx_remove_sysfs(struct qlcnic_adapter *adapter)
1433{
1434	struct device *dev = &adapter->pdev->dev;
1435
1436	qlcnic_remove_diag_entries(adapter);
1437	sysfs_remove_bin_file(&dev->kobj, &bin_attr_flash);
1438}
1439