hinic_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Huawei HiNIC PCI Express Linux driver
  3. * Copyright(c) 2017 Huawei Technologies Co., Ltd
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/pci.h>
  19. #include <linux/device.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/slab.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/workqueue.h>
  28. #include <net/ip.h>
  29. #include <linux/bitops.h>
  30. #include <linux/bitmap.h>
  31. #include <linux/delay.h>
  32. #include <linux/err.h>
  33. #include "hinic_hw_qp.h"
  34. #include "hinic_hw_dev.h"
  35. #include "hinic_port.h"
  36. #include "hinic_tx.h"
  37. #include "hinic_rx.h"
  38. #include "hinic_dev.h"
  39. MODULE_AUTHOR("Huawei Technologies CO., Ltd");
  40. MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
  41. MODULE_LICENSE("GPL");
  42. static unsigned int tx_weight = 64;
  43. module_param(tx_weight, uint, 0644);
  44. MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
  45. static unsigned int rx_weight = 64;
  46. module_param(rx_weight, uint, 0644);
  47. MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
  48. #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
  49. #define HINIC_DEV_ID_DUAL_PORT_25GE 0x0200
  50. #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0201
  51. #define HINIC_WQ_NAME "hinic_dev"
  52. #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  53. NETIF_MSG_IFUP | \
  54. NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
  55. #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
  56. #define work_to_rx_mode_work(work) \
  57. container_of(work, struct hinic_rx_mode_work, work)
  58. #define rx_mode_work_to_nic_dev(rx_mode_work) \
  59. container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
  60. static int change_mac_addr(struct net_device *netdev, const u8 *addr);
  61. static void set_link_speed(struct ethtool_link_ksettings *link_ksettings,
  62. enum hinic_speed speed)
  63. {
  64. switch (speed) {
  65. case HINIC_SPEED_10MB_LINK:
  66. link_ksettings->base.speed = SPEED_10;
  67. break;
  68. case HINIC_SPEED_100MB_LINK:
  69. link_ksettings->base.speed = SPEED_100;
  70. break;
  71. case HINIC_SPEED_1000MB_LINK:
  72. link_ksettings->base.speed = SPEED_1000;
  73. break;
  74. case HINIC_SPEED_10GB_LINK:
  75. link_ksettings->base.speed = SPEED_10000;
  76. break;
  77. case HINIC_SPEED_25GB_LINK:
  78. link_ksettings->base.speed = SPEED_25000;
  79. break;
  80. case HINIC_SPEED_40GB_LINK:
  81. link_ksettings->base.speed = SPEED_40000;
  82. break;
  83. case HINIC_SPEED_100GB_LINK:
  84. link_ksettings->base.speed = SPEED_100000;
  85. break;
  86. default:
  87. link_ksettings->base.speed = SPEED_UNKNOWN;
  88. break;
  89. }
  90. }
  91. static int hinic_get_link_ksettings(struct net_device *netdev,
  92. struct ethtool_link_ksettings
  93. *link_ksettings)
  94. {
  95. struct hinic_dev *nic_dev = netdev_priv(netdev);
  96. enum hinic_port_link_state link_state;
  97. struct hinic_port_cap port_cap;
  98. int err;
  99. ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
  100. ethtool_link_ksettings_add_link_mode(link_ksettings, supported,
  101. Autoneg);
  102. link_ksettings->base.speed = SPEED_UNKNOWN;
  103. link_ksettings->base.autoneg = AUTONEG_DISABLE;
  104. link_ksettings->base.duplex = DUPLEX_UNKNOWN;
  105. err = hinic_port_get_cap(nic_dev, &port_cap);
  106. if (err) {
  107. netif_err(nic_dev, drv, netdev,
  108. "Failed to get port capabilities\n");
  109. return err;
  110. }
  111. err = hinic_port_link_state(nic_dev, &link_state);
  112. if (err) {
  113. netif_err(nic_dev, drv, netdev,
  114. "Failed to get port link state\n");
  115. return err;
  116. }
  117. if (link_state != HINIC_LINK_STATE_UP) {
  118. netif_info(nic_dev, drv, netdev, "No link\n");
  119. return err;
  120. }
  121. set_link_speed(link_ksettings, port_cap.speed);
  122. if (!!(port_cap.autoneg_cap & HINIC_AUTONEG_SUPPORTED))
  123. ethtool_link_ksettings_add_link_mode(link_ksettings,
  124. advertising, Autoneg);
  125. if (port_cap.autoneg_state == HINIC_AUTONEG_ACTIVE)
  126. link_ksettings->base.autoneg = AUTONEG_ENABLE;
  127. link_ksettings->base.duplex = (port_cap.duplex == HINIC_DUPLEX_FULL) ?
  128. DUPLEX_FULL : DUPLEX_HALF;
  129. return 0;
  130. }
  131. static void hinic_get_drvinfo(struct net_device *netdev,
  132. struct ethtool_drvinfo *info)
  133. {
  134. struct hinic_dev *nic_dev = netdev_priv(netdev);
  135. struct hinic_hwdev *hwdev = nic_dev->hwdev;
  136. struct hinic_hwif *hwif = hwdev->hwif;
  137. strlcpy(info->driver, HINIC_DRV_NAME, sizeof(info->driver));
  138. strlcpy(info->bus_info, pci_name(hwif->pdev), sizeof(info->bus_info));
  139. }
  140. static void hinic_get_ringparam(struct net_device *netdev,
  141. struct ethtool_ringparam *ring)
  142. {
  143. ring->rx_max_pending = HINIC_RQ_DEPTH;
  144. ring->tx_max_pending = HINIC_SQ_DEPTH;
  145. ring->rx_pending = HINIC_RQ_DEPTH;
  146. ring->tx_pending = HINIC_SQ_DEPTH;
  147. }
  148. static void hinic_get_channels(struct net_device *netdev,
  149. struct ethtool_channels *channels)
  150. {
  151. struct hinic_dev *nic_dev = netdev_priv(netdev);
  152. struct hinic_hwdev *hwdev = nic_dev->hwdev;
  153. channels->max_rx = hwdev->nic_cap.max_qps;
  154. channels->max_tx = hwdev->nic_cap.max_qps;
  155. channels->max_other = 0;
  156. channels->max_combined = 0;
  157. channels->rx_count = hinic_hwdev_num_qps(hwdev);
  158. channels->tx_count = hinic_hwdev_num_qps(hwdev);
  159. channels->other_count = 0;
  160. channels->combined_count = 0;
  161. }
  162. static const struct ethtool_ops hinic_ethtool_ops = {
  163. .get_link_ksettings = hinic_get_link_ksettings,
  164. .get_drvinfo = hinic_get_drvinfo,
  165. .get_link = ethtool_op_get_link,
  166. .get_ringparam = hinic_get_ringparam,
  167. .get_channels = hinic_get_channels,
  168. };
  169. static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
  170. {
  171. struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
  172. struct hinic_rxq_stats rx_stats;
  173. u64_stats_init(&rx_stats.syncp);
  174. hinic_rxq_get_stats(rxq, &rx_stats);
  175. u64_stats_update_begin(&nic_rx_stats->syncp);
  176. nic_rx_stats->bytes += rx_stats.bytes;
  177. nic_rx_stats->pkts += rx_stats.pkts;
  178. u64_stats_update_end(&nic_rx_stats->syncp);
  179. hinic_rxq_clean_stats(rxq);
  180. }
  181. static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
  182. {
  183. struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
  184. struct hinic_txq_stats tx_stats;
  185. u64_stats_init(&tx_stats.syncp);
  186. hinic_txq_get_stats(txq, &tx_stats);
  187. u64_stats_update_begin(&nic_tx_stats->syncp);
  188. nic_tx_stats->bytes += tx_stats.bytes;
  189. nic_tx_stats->pkts += tx_stats.pkts;
  190. nic_tx_stats->tx_busy += tx_stats.tx_busy;
  191. nic_tx_stats->tx_wake += tx_stats.tx_wake;
  192. nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
  193. u64_stats_update_end(&nic_tx_stats->syncp);
  194. hinic_txq_clean_stats(txq);
  195. }
  196. static void update_nic_stats(struct hinic_dev *nic_dev)
  197. {
  198. int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
  199. for (i = 0; i < num_qps; i++)
  200. update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
  201. for (i = 0; i < num_qps; i++)
  202. update_tx_stats(nic_dev, &nic_dev->txqs[i]);
  203. }
  204. /**
  205. * create_txqs - Create the Logical Tx Queues of specific NIC device
  206. * @nic_dev: the specific NIC device
  207. *
  208. * Return 0 - Success, negative - Failure
  209. **/
  210. static int create_txqs(struct hinic_dev *nic_dev)
  211. {
  212. int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
  213. struct net_device *netdev = nic_dev->netdev;
  214. size_t txq_size;
  215. if (nic_dev->txqs)
  216. return -EINVAL;
  217. txq_size = num_txqs * sizeof(*nic_dev->txqs);
  218. nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
  219. if (!nic_dev->txqs)
  220. return -ENOMEM;
  221. for (i = 0; i < num_txqs; i++) {
  222. struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
  223. err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
  224. if (err) {
  225. netif_err(nic_dev, drv, netdev,
  226. "Failed to init Txq\n");
  227. goto err_init_txq;
  228. }
  229. }
  230. return 0;
  231. err_init_txq:
  232. for (j = 0; j < i; j++)
  233. hinic_clean_txq(&nic_dev->txqs[j]);
  234. devm_kfree(&netdev->dev, nic_dev->txqs);
  235. return err;
  236. }
  237. /**
  238. * free_txqs - Free the Logical Tx Queues of specific NIC device
  239. * @nic_dev: the specific NIC device
  240. **/
  241. static void free_txqs(struct hinic_dev *nic_dev)
  242. {
  243. int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
  244. struct net_device *netdev = nic_dev->netdev;
  245. if (!nic_dev->txqs)
  246. return;
  247. for (i = 0; i < num_txqs; i++)
  248. hinic_clean_txq(&nic_dev->txqs[i]);
  249. devm_kfree(&netdev->dev, nic_dev->txqs);
  250. nic_dev->txqs = NULL;
  251. }
  252. /**
  253. * create_txqs - Create the Logical Rx Queues of specific NIC device
  254. * @nic_dev: the specific NIC device
  255. *
  256. * Return 0 - Success, negative - Failure
  257. **/
  258. static int create_rxqs(struct hinic_dev *nic_dev)
  259. {
  260. int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
  261. struct net_device *netdev = nic_dev->netdev;
  262. size_t rxq_size;
  263. if (nic_dev->rxqs)
  264. return -EINVAL;
  265. rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
  266. nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
  267. if (!nic_dev->rxqs)
  268. return -ENOMEM;
  269. for (i = 0; i < num_rxqs; i++) {
  270. struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
  271. err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
  272. if (err) {
  273. netif_err(nic_dev, drv, netdev,
  274. "Failed to init rxq\n");
  275. goto err_init_rxq;
  276. }
  277. }
  278. return 0;
  279. err_init_rxq:
  280. for (j = 0; j < i; j++)
  281. hinic_clean_rxq(&nic_dev->rxqs[j]);
  282. devm_kfree(&netdev->dev, nic_dev->rxqs);
  283. return err;
  284. }
  285. /**
  286. * free_txqs - Free the Logical Rx Queues of specific NIC device
  287. * @nic_dev: the specific NIC device
  288. **/
  289. static void free_rxqs(struct hinic_dev *nic_dev)
  290. {
  291. int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
  292. struct net_device *netdev = nic_dev->netdev;
  293. if (!nic_dev->rxqs)
  294. return;
  295. for (i = 0; i < num_rxqs; i++)
  296. hinic_clean_rxq(&nic_dev->rxqs[i]);
  297. devm_kfree(&netdev->dev, nic_dev->rxqs);
  298. nic_dev->rxqs = NULL;
  299. }
  300. static int hinic_open(struct net_device *netdev)
  301. {
  302. struct hinic_dev *nic_dev = netdev_priv(netdev);
  303. enum hinic_port_link_state link_state;
  304. int err, ret, num_qps;
  305. if (!(nic_dev->flags & HINIC_INTF_UP)) {
  306. err = hinic_hwdev_ifup(nic_dev->hwdev);
  307. if (err) {
  308. netif_err(nic_dev, drv, netdev,
  309. "Failed - HW interface up\n");
  310. return err;
  311. }
  312. }
  313. err = create_txqs(nic_dev);
  314. if (err) {
  315. netif_err(nic_dev, drv, netdev,
  316. "Failed to create Tx queues\n");
  317. goto err_create_txqs;
  318. }
  319. err = create_rxqs(nic_dev);
  320. if (err) {
  321. netif_err(nic_dev, drv, netdev,
  322. "Failed to create Rx queues\n");
  323. goto err_create_rxqs;
  324. }
  325. num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
  326. netif_set_real_num_tx_queues(netdev, num_qps);
  327. netif_set_real_num_rx_queues(netdev, num_qps);
  328. err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
  329. if (err) {
  330. netif_err(nic_dev, drv, netdev,
  331. "Failed to set port state\n");
  332. goto err_port_state;
  333. }
  334. err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
  335. if (err) {
  336. netif_err(nic_dev, drv, netdev,
  337. "Failed to set func port state\n");
  338. goto err_func_port_state;
  339. }
  340. /* Wait up to 3 sec between port enable to link state */
  341. msleep(3000);
  342. down(&nic_dev->mgmt_lock);
  343. err = hinic_port_link_state(nic_dev, &link_state);
  344. if (err) {
  345. netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
  346. goto err_port_link;
  347. }
  348. if (link_state == HINIC_LINK_STATE_UP)
  349. nic_dev->flags |= HINIC_LINK_UP;
  350. nic_dev->flags |= HINIC_INTF_UP;
  351. if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
  352. (HINIC_LINK_UP | HINIC_INTF_UP)) {
  353. netif_info(nic_dev, drv, netdev, "link + intf UP\n");
  354. netif_carrier_on(netdev);
  355. netif_tx_wake_all_queues(netdev);
  356. }
  357. up(&nic_dev->mgmt_lock);
  358. netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
  359. return 0;
  360. err_port_link:
  361. up(&nic_dev->mgmt_lock);
  362. ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
  363. if (ret)
  364. netif_warn(nic_dev, drv, netdev,
  365. "Failed to revert func port state\n");
  366. err_func_port_state:
  367. ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
  368. if (ret)
  369. netif_warn(nic_dev, drv, netdev,
  370. "Failed to revert port state\n");
  371. err_port_state:
  372. free_rxqs(nic_dev);
  373. err_create_rxqs:
  374. free_txqs(nic_dev);
  375. err_create_txqs:
  376. if (!(nic_dev->flags & HINIC_INTF_UP))
  377. hinic_hwdev_ifdown(nic_dev->hwdev);
  378. return err;
  379. }
  380. static int hinic_close(struct net_device *netdev)
  381. {
  382. struct hinic_dev *nic_dev = netdev_priv(netdev);
  383. unsigned int flags;
  384. int err;
  385. down(&nic_dev->mgmt_lock);
  386. flags = nic_dev->flags;
  387. nic_dev->flags &= ~HINIC_INTF_UP;
  388. netif_carrier_off(netdev);
  389. netif_tx_disable(netdev);
  390. update_nic_stats(nic_dev);
  391. up(&nic_dev->mgmt_lock);
  392. err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
  393. if (err) {
  394. netif_err(nic_dev, drv, netdev,
  395. "Failed to set func port state\n");
  396. nic_dev->flags |= (flags & HINIC_INTF_UP);
  397. return err;
  398. }
  399. err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
  400. if (err) {
  401. netif_err(nic_dev, drv, netdev, "Failed to set port state\n");
  402. nic_dev->flags |= (flags & HINIC_INTF_UP);
  403. return err;
  404. }
  405. free_rxqs(nic_dev);
  406. free_txqs(nic_dev);
  407. if (flags & HINIC_INTF_UP)
  408. hinic_hwdev_ifdown(nic_dev->hwdev);
  409. netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
  410. return 0;
  411. }
  412. static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
  413. {
  414. struct hinic_dev *nic_dev = netdev_priv(netdev);
  415. int err;
  416. netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
  417. err = hinic_port_set_mtu(nic_dev, new_mtu);
  418. if (err)
  419. netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
  420. else
  421. netdev->mtu = new_mtu;
  422. return err;
  423. }
  424. /**
  425. * change_mac_addr - change the main mac address of network device
  426. * @netdev: network device
  427. * @addr: mac address to set
  428. *
  429. * Return 0 - Success, negative - Failure
  430. **/
  431. static int change_mac_addr(struct net_device *netdev, const u8 *addr)
  432. {
  433. struct hinic_dev *nic_dev = netdev_priv(netdev);
  434. u16 vid = 0;
  435. int err;
  436. if (!is_valid_ether_addr(addr))
  437. return -EADDRNOTAVAIL;
  438. netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
  439. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  440. down(&nic_dev->mgmt_lock);
  441. do {
  442. err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
  443. if (err) {
  444. netif_err(nic_dev, drv, netdev,
  445. "Failed to delete mac\n");
  446. break;
  447. }
  448. err = hinic_port_add_mac(nic_dev, addr, vid);
  449. if (err) {
  450. netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
  451. break;
  452. }
  453. vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
  454. } while (vid != VLAN_N_VID);
  455. up(&nic_dev->mgmt_lock);
  456. return err;
  457. }
  458. static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
  459. {
  460. unsigned char new_mac[ETH_ALEN];
  461. struct sockaddr *saddr = addr;
  462. int err;
  463. memcpy(new_mac, saddr->sa_data, ETH_ALEN);
  464. err = change_mac_addr(netdev, new_mac);
  465. if (!err)
  466. memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
  467. return err;
  468. }
  469. /**
  470. * add_mac_addr - add mac address to network device
  471. * @netdev: network device
  472. * @addr: mac address to add
  473. *
  474. * Return 0 - Success, negative - Failure
  475. **/
  476. static int add_mac_addr(struct net_device *netdev, const u8 *addr)
  477. {
  478. struct hinic_dev *nic_dev = netdev_priv(netdev);
  479. u16 vid = 0;
  480. int err;
  481. netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
  482. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  483. down(&nic_dev->mgmt_lock);
  484. do {
  485. err = hinic_port_add_mac(nic_dev, addr, vid);
  486. if (err) {
  487. netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
  488. break;
  489. }
  490. vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
  491. } while (vid != VLAN_N_VID);
  492. up(&nic_dev->mgmt_lock);
  493. return err;
  494. }
  495. /**
  496. * remove_mac_addr - remove mac address from network device
  497. * @netdev: network device
  498. * @addr: mac address to remove
  499. *
  500. * Return 0 - Success, negative - Failure
  501. **/
  502. static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
  503. {
  504. struct hinic_dev *nic_dev = netdev_priv(netdev);
  505. u16 vid = 0;
  506. int err;
  507. if (!is_valid_ether_addr(addr))
  508. return -EADDRNOTAVAIL;
  509. netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
  510. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  511. down(&nic_dev->mgmt_lock);
  512. do {
  513. err = hinic_port_del_mac(nic_dev, addr, vid);
  514. if (err) {
  515. netif_err(nic_dev, drv, netdev,
  516. "Failed to delete mac\n");
  517. break;
  518. }
  519. vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
  520. } while (vid != VLAN_N_VID);
  521. up(&nic_dev->mgmt_lock);
  522. return err;
  523. }
  524. static int hinic_vlan_rx_add_vid(struct net_device *netdev,
  525. __always_unused __be16 proto, u16 vid)
  526. {
  527. struct hinic_dev *nic_dev = netdev_priv(netdev);
  528. int ret, err;
  529. netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
  530. down(&nic_dev->mgmt_lock);
  531. err = hinic_port_add_vlan(nic_dev, vid);
  532. if (err) {
  533. netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
  534. goto err_vlan_add;
  535. }
  536. err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
  537. if (err) {
  538. netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
  539. goto err_add_mac;
  540. }
  541. bitmap_set(nic_dev->vlan_bitmap, vid, 1);
  542. up(&nic_dev->mgmt_lock);
  543. return 0;
  544. err_add_mac:
  545. ret = hinic_port_del_vlan(nic_dev, vid);
  546. if (ret)
  547. netif_err(nic_dev, drv, netdev,
  548. "Failed to revert by removing vlan\n");
  549. err_vlan_add:
  550. up(&nic_dev->mgmt_lock);
  551. return err;
  552. }
  553. static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
  554. __always_unused __be16 proto, u16 vid)
  555. {
  556. struct hinic_dev *nic_dev = netdev_priv(netdev);
  557. int err;
  558. netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
  559. down(&nic_dev->mgmt_lock);
  560. err = hinic_port_del_vlan(nic_dev, vid);
  561. if (err) {
  562. netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
  563. goto err_del_vlan;
  564. }
  565. bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
  566. up(&nic_dev->mgmt_lock);
  567. return 0;
  568. err_del_vlan:
  569. up(&nic_dev->mgmt_lock);
  570. return err;
  571. }
  572. static void set_rx_mode(struct work_struct *work)
  573. {
  574. struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
  575. struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
  576. struct netdev_hw_addr *ha;
  577. netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
  578. hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
  579. __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
  580. __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
  581. netdev_for_each_mc_addr(ha, nic_dev->netdev)
  582. add_mac_addr(nic_dev->netdev, ha->addr);
  583. }
  584. static void hinic_set_rx_mode(struct net_device *netdev)
  585. {
  586. struct hinic_dev *nic_dev = netdev_priv(netdev);
  587. struct hinic_rx_mode_work *rx_mode_work;
  588. u32 rx_mode;
  589. rx_mode_work = &nic_dev->rx_mode_work;
  590. rx_mode = HINIC_RX_MODE_UC |
  591. HINIC_RX_MODE_MC |
  592. HINIC_RX_MODE_BC;
  593. if (netdev->flags & IFF_PROMISC)
  594. rx_mode |= HINIC_RX_MODE_PROMISC;
  595. else if (netdev->flags & IFF_ALLMULTI)
  596. rx_mode |= HINIC_RX_MODE_MC_ALL;
  597. rx_mode_work->rx_mode = rx_mode;
  598. queue_work(nic_dev->workq, &rx_mode_work->work);
  599. }
  600. static void hinic_tx_timeout(struct net_device *netdev)
  601. {
  602. struct hinic_dev *nic_dev = netdev_priv(netdev);
  603. netif_err(nic_dev, drv, netdev, "Tx timeout\n");
  604. }
  605. static void hinic_get_stats64(struct net_device *netdev,
  606. struct rtnl_link_stats64 *stats)
  607. {
  608. struct hinic_dev *nic_dev = netdev_priv(netdev);
  609. struct hinic_rxq_stats *nic_rx_stats;
  610. struct hinic_txq_stats *nic_tx_stats;
  611. nic_rx_stats = &nic_dev->rx_stats;
  612. nic_tx_stats = &nic_dev->tx_stats;
  613. down(&nic_dev->mgmt_lock);
  614. if (nic_dev->flags & HINIC_INTF_UP)
  615. update_nic_stats(nic_dev);
  616. up(&nic_dev->mgmt_lock);
  617. stats->rx_bytes = nic_rx_stats->bytes;
  618. stats->rx_packets = nic_rx_stats->pkts;
  619. stats->tx_bytes = nic_tx_stats->bytes;
  620. stats->tx_packets = nic_tx_stats->pkts;
  621. stats->tx_errors = nic_tx_stats->tx_dropped;
  622. }
  623. static const struct net_device_ops hinic_netdev_ops = {
  624. .ndo_open = hinic_open,
  625. .ndo_stop = hinic_close,
  626. .ndo_change_mtu = hinic_change_mtu,
  627. .ndo_set_mac_address = hinic_set_mac_addr,
  628. .ndo_validate_addr = eth_validate_addr,
  629. .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
  630. .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
  631. .ndo_set_rx_mode = hinic_set_rx_mode,
  632. .ndo_start_xmit = hinic_xmit_frame,
  633. .ndo_tx_timeout = hinic_tx_timeout,
  634. .ndo_get_stats64 = hinic_get_stats64,
  635. };
  636. static void netdev_features_init(struct net_device *netdev)
  637. {
  638. netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
  639. netdev->vlan_features = netdev->hw_features;
  640. netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  641. }
  642. /**
  643. * link_status_event_handler - link event handler
  644. * @handle: nic device for the handler
  645. * @buf_in: input buffer
  646. * @in_size: input size
  647. * @buf_in: output buffer
  648. * @out_size: returned output size
  649. *
  650. * Return 0 - Success, negative - Failure
  651. **/
  652. static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
  653. void *buf_out, u16 *out_size)
  654. {
  655. struct hinic_port_link_status *link_status, *ret_link_status;
  656. struct hinic_dev *nic_dev = handle;
  657. link_status = buf_in;
  658. if (link_status->link == HINIC_LINK_STATE_UP) {
  659. down(&nic_dev->mgmt_lock);
  660. nic_dev->flags |= HINIC_LINK_UP;
  661. if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
  662. (HINIC_LINK_UP | HINIC_INTF_UP)) {
  663. netif_carrier_on(nic_dev->netdev);
  664. netif_tx_wake_all_queues(nic_dev->netdev);
  665. }
  666. up(&nic_dev->mgmt_lock);
  667. netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
  668. } else {
  669. down(&nic_dev->mgmt_lock);
  670. nic_dev->flags &= ~HINIC_LINK_UP;
  671. netif_carrier_off(nic_dev->netdev);
  672. netif_tx_disable(nic_dev->netdev);
  673. up(&nic_dev->mgmt_lock);
  674. netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
  675. }
  676. ret_link_status = buf_out;
  677. ret_link_status->status = 0;
  678. *out_size = sizeof(*ret_link_status);
  679. }
  680. /**
  681. * nic_dev_init - Initialize the NIC device
  682. * @pdev: the NIC pci device
  683. *
  684. * Return 0 - Success, negative - Failure
  685. **/
  686. static int nic_dev_init(struct pci_dev *pdev)
  687. {
  688. struct hinic_rx_mode_work *rx_mode_work;
  689. struct hinic_txq_stats *tx_stats;
  690. struct hinic_rxq_stats *rx_stats;
  691. struct hinic_dev *nic_dev;
  692. struct net_device *netdev;
  693. struct hinic_hwdev *hwdev;
  694. int err, num_qps;
  695. hwdev = hinic_init_hwdev(pdev);
  696. if (IS_ERR(hwdev)) {
  697. dev_err(&pdev->dev, "Failed to initialize HW device\n");
  698. return PTR_ERR(hwdev);
  699. }
  700. num_qps = hinic_hwdev_num_qps(hwdev);
  701. if (num_qps <= 0) {
  702. dev_err(&pdev->dev, "Invalid number of QPS\n");
  703. err = -EINVAL;
  704. goto err_num_qps;
  705. }
  706. netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
  707. if (!netdev) {
  708. dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
  709. err = -ENOMEM;
  710. goto err_alloc_etherdev;
  711. }
  712. netdev->netdev_ops = &hinic_netdev_ops;
  713. netdev->ethtool_ops = &hinic_ethtool_ops;
  714. netdev->max_mtu = ETH_MAX_MTU;
  715. nic_dev = netdev_priv(netdev);
  716. nic_dev->netdev = netdev;
  717. nic_dev->hwdev = hwdev;
  718. nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
  719. nic_dev->flags = 0;
  720. nic_dev->txqs = NULL;
  721. nic_dev->rxqs = NULL;
  722. nic_dev->tx_weight = tx_weight;
  723. nic_dev->rx_weight = rx_weight;
  724. sema_init(&nic_dev->mgmt_lock, 1);
  725. tx_stats = &nic_dev->tx_stats;
  726. rx_stats = &nic_dev->rx_stats;
  727. u64_stats_init(&tx_stats->syncp);
  728. u64_stats_init(&rx_stats->syncp);
  729. nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
  730. VLAN_BITMAP_SIZE(nic_dev),
  731. GFP_KERNEL);
  732. if (!nic_dev->vlan_bitmap) {
  733. err = -ENOMEM;
  734. goto err_vlan_bitmap;
  735. }
  736. nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
  737. if (!nic_dev->workq) {
  738. err = -ENOMEM;
  739. goto err_workq;
  740. }
  741. pci_set_drvdata(pdev, netdev);
  742. err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
  743. if (err)
  744. dev_warn(&pdev->dev, "Failed to get mac address\n");
  745. err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
  746. if (err) {
  747. dev_err(&pdev->dev, "Failed to add mac\n");
  748. goto err_add_mac;
  749. }
  750. err = hinic_port_set_mtu(nic_dev, netdev->mtu);
  751. if (err) {
  752. dev_err(&pdev->dev, "Failed to set mtu\n");
  753. goto err_set_mtu;
  754. }
  755. rx_mode_work = &nic_dev->rx_mode_work;
  756. INIT_WORK(&rx_mode_work->work, set_rx_mode);
  757. netdev_features_init(netdev);
  758. netif_carrier_off(netdev);
  759. hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
  760. nic_dev, link_status_event_handler);
  761. SET_NETDEV_DEV(netdev, &pdev->dev);
  762. err = register_netdev(netdev);
  763. if (err) {
  764. dev_err(&pdev->dev, "Failed to register netdev\n");
  765. goto err_reg_netdev;
  766. }
  767. return 0;
  768. err_reg_netdev:
  769. hinic_hwdev_cb_unregister(nic_dev->hwdev,
  770. HINIC_MGMT_MSG_CMD_LINK_STATUS);
  771. cancel_work_sync(&rx_mode_work->work);
  772. err_set_mtu:
  773. err_add_mac:
  774. pci_set_drvdata(pdev, NULL);
  775. destroy_workqueue(nic_dev->workq);
  776. err_workq:
  777. err_vlan_bitmap:
  778. free_netdev(netdev);
  779. err_alloc_etherdev:
  780. err_num_qps:
  781. hinic_free_hwdev(hwdev);
  782. return err;
  783. }
  784. static int hinic_probe(struct pci_dev *pdev,
  785. const struct pci_device_id *id)
  786. {
  787. int err = pci_enable_device(pdev);
  788. if (err) {
  789. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  790. return err;
  791. }
  792. err = pci_request_regions(pdev, HINIC_DRV_NAME);
  793. if (err) {
  794. dev_err(&pdev->dev, "Failed to request PCI regions\n");
  795. goto err_pci_regions;
  796. }
  797. pci_set_master(pdev);
  798. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  799. if (err) {
  800. dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
  801. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  802. if (err) {
  803. dev_err(&pdev->dev, "Failed to set DMA mask\n");
  804. goto err_dma_mask;
  805. }
  806. }
  807. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  808. if (err) {
  809. dev_warn(&pdev->dev,
  810. "Couldn't set 64-bit consistent DMA mask\n");
  811. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  812. if (err) {
  813. dev_err(&pdev->dev,
  814. "Failed to set consistent DMA mask\n");
  815. goto err_dma_consistent_mask;
  816. }
  817. }
  818. err = nic_dev_init(pdev);
  819. if (err) {
  820. dev_err(&pdev->dev, "Failed to initialize NIC device\n");
  821. goto err_nic_dev_init;
  822. }
  823. dev_info(&pdev->dev, "HiNIC driver - probed\n");
  824. return 0;
  825. err_nic_dev_init:
  826. err_dma_consistent_mask:
  827. err_dma_mask:
  828. pci_release_regions(pdev);
  829. err_pci_regions:
  830. pci_disable_device(pdev);
  831. return err;
  832. }
  833. static void hinic_remove(struct pci_dev *pdev)
  834. {
  835. struct net_device *netdev = pci_get_drvdata(pdev);
  836. struct hinic_dev *nic_dev = netdev_priv(netdev);
  837. struct hinic_rx_mode_work *rx_mode_work;
  838. unregister_netdev(netdev);
  839. hinic_hwdev_cb_unregister(nic_dev->hwdev,
  840. HINIC_MGMT_MSG_CMD_LINK_STATUS);
  841. rx_mode_work = &nic_dev->rx_mode_work;
  842. cancel_work_sync(&rx_mode_work->work);
  843. pci_set_drvdata(pdev, NULL);
  844. destroy_workqueue(nic_dev->workq);
  845. hinic_free_hwdev(nic_dev->hwdev);
  846. free_netdev(netdev);
  847. pci_release_regions(pdev);
  848. pci_disable_device(pdev);
  849. dev_info(&pdev->dev, "HiNIC driver - removed\n");
  850. }
  851. static const struct pci_device_id hinic_pci_table[] = {
  852. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
  853. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_25GE), 0},
  854. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
  855. { 0, 0}
  856. };
  857. MODULE_DEVICE_TABLE(pci, hinic_pci_table);
  858. static struct pci_driver hinic_driver = {
  859. .name = HINIC_DRV_NAME,
  860. .id_table = hinic_pci_table,
  861. .probe = hinic_probe,
  862. .remove = hinic_remove,
  863. };
  864. module_pci_driver(hinic_driver);