qlcnic_sysfs.c 37 KB

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