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