hinic_main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. if (!is_valid_ether_addr(addr))
  482. return -EADDRNOTAVAIL;
  483. netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
  484. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  485. down(&nic_dev->mgmt_lock);
  486. do {
  487. err = hinic_port_add_mac(nic_dev, addr, vid);
  488. if (err) {
  489. netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
  490. break;
  491. }
  492. vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
  493. } while (vid != VLAN_N_VID);
  494. up(&nic_dev->mgmt_lock);
  495. return err;
  496. }
  497. /**
  498. * remove_mac_addr - remove mac address from network device
  499. * @netdev: network device
  500. * @addr: mac address to remove
  501. *
  502. * Return 0 - Success, negative - Failure
  503. **/
  504. static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
  505. {
  506. struct hinic_dev *nic_dev = netdev_priv(netdev);
  507. u16 vid = 0;
  508. int err;
  509. if (!is_valid_ether_addr(addr))
  510. return -EADDRNOTAVAIL;
  511. netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
  512. addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  513. down(&nic_dev->mgmt_lock);
  514. do {
  515. err = hinic_port_del_mac(nic_dev, addr, vid);
  516. if (err) {
  517. netif_err(nic_dev, drv, netdev,
  518. "Failed to delete mac\n");
  519. break;
  520. }
  521. vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
  522. } while (vid != VLAN_N_VID);
  523. up(&nic_dev->mgmt_lock);
  524. return err;
  525. }
  526. static int hinic_vlan_rx_add_vid(struct net_device *netdev,
  527. __always_unused __be16 proto, u16 vid)
  528. {
  529. struct hinic_dev *nic_dev = netdev_priv(netdev);
  530. int ret, err;
  531. netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
  532. down(&nic_dev->mgmt_lock);
  533. err = hinic_port_add_vlan(nic_dev, vid);
  534. if (err) {
  535. netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
  536. goto err_vlan_add;
  537. }
  538. err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
  539. if (err) {
  540. netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
  541. goto err_add_mac;
  542. }
  543. bitmap_set(nic_dev->vlan_bitmap, vid, 1);
  544. up(&nic_dev->mgmt_lock);
  545. return 0;
  546. err_add_mac:
  547. ret = hinic_port_del_vlan(nic_dev, vid);
  548. if (ret)
  549. netif_err(nic_dev, drv, netdev,
  550. "Failed to revert by removing vlan\n");
  551. err_vlan_add:
  552. up(&nic_dev->mgmt_lock);
  553. return err;
  554. }
  555. static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
  556. __always_unused __be16 proto, u16 vid)
  557. {
  558. struct hinic_dev *nic_dev = netdev_priv(netdev);
  559. int err;
  560. netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
  561. down(&nic_dev->mgmt_lock);
  562. err = hinic_port_del_vlan(nic_dev, vid);
  563. if (err) {
  564. netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
  565. goto err_del_vlan;
  566. }
  567. bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
  568. up(&nic_dev->mgmt_lock);
  569. return 0;
  570. err_del_vlan:
  571. up(&nic_dev->mgmt_lock);
  572. return err;
  573. }
  574. static void set_rx_mode(struct work_struct *work)
  575. {
  576. struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
  577. struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
  578. netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
  579. hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
  580. __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
  581. __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
  582. }
  583. static void hinic_set_rx_mode(struct net_device *netdev)
  584. {
  585. struct hinic_dev *nic_dev = netdev_priv(netdev);
  586. struct hinic_rx_mode_work *rx_mode_work;
  587. u32 rx_mode;
  588. rx_mode_work = &nic_dev->rx_mode_work;
  589. rx_mode = HINIC_RX_MODE_UC |
  590. HINIC_RX_MODE_MC |
  591. HINIC_RX_MODE_BC;
  592. if (netdev->flags & IFF_PROMISC)
  593. rx_mode |= HINIC_RX_MODE_PROMISC;
  594. else if (netdev->flags & IFF_ALLMULTI)
  595. rx_mode |= HINIC_RX_MODE_MC_ALL;
  596. rx_mode_work->rx_mode = rx_mode;
  597. queue_work(nic_dev->workq, &rx_mode_work->work);
  598. }
  599. static void hinic_tx_timeout(struct net_device *netdev)
  600. {
  601. struct hinic_dev *nic_dev = netdev_priv(netdev);
  602. netif_err(nic_dev, drv, netdev, "Tx timeout\n");
  603. }
  604. static void hinic_get_stats64(struct net_device *netdev,
  605. struct rtnl_link_stats64 *stats)
  606. {
  607. struct hinic_dev *nic_dev = netdev_priv(netdev);
  608. struct hinic_rxq_stats *nic_rx_stats;
  609. struct hinic_txq_stats *nic_tx_stats;
  610. nic_rx_stats = &nic_dev->rx_stats;
  611. nic_tx_stats = &nic_dev->tx_stats;
  612. down(&nic_dev->mgmt_lock);
  613. if (nic_dev->flags & HINIC_INTF_UP)
  614. update_nic_stats(nic_dev);
  615. up(&nic_dev->mgmt_lock);
  616. stats->rx_bytes = nic_rx_stats->bytes;
  617. stats->rx_packets = nic_rx_stats->pkts;
  618. stats->tx_bytes = nic_tx_stats->bytes;
  619. stats->tx_packets = nic_tx_stats->pkts;
  620. stats->tx_errors = nic_tx_stats->tx_dropped;
  621. }
  622. #ifdef CONFIG_NET_POLL_CONTROLLER
  623. static void hinic_netpoll(struct net_device *netdev)
  624. {
  625. struct hinic_dev *nic_dev = netdev_priv(netdev);
  626. int i, num_qps;
  627. num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
  628. for (i = 0; i < num_qps; i++) {
  629. struct hinic_txq *txq = &nic_dev->txqs[i];
  630. struct hinic_rxq *rxq = &nic_dev->rxqs[i];
  631. napi_schedule(&txq->napi);
  632. napi_schedule(&rxq->napi);
  633. }
  634. }
  635. #endif
  636. static const struct net_device_ops hinic_netdev_ops = {
  637. .ndo_open = hinic_open,
  638. .ndo_stop = hinic_close,
  639. .ndo_change_mtu = hinic_change_mtu,
  640. .ndo_set_mac_address = hinic_set_mac_addr,
  641. .ndo_validate_addr = eth_validate_addr,
  642. .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
  643. .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
  644. .ndo_set_rx_mode = hinic_set_rx_mode,
  645. .ndo_start_xmit = hinic_xmit_frame,
  646. .ndo_tx_timeout = hinic_tx_timeout,
  647. .ndo_get_stats64 = hinic_get_stats64,
  648. #ifdef CONFIG_NET_POLL_CONTROLLER
  649. .ndo_poll_controller = hinic_netpoll,
  650. #endif
  651. };
  652. static void netdev_features_init(struct net_device *netdev)
  653. {
  654. netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
  655. netdev->vlan_features = netdev->hw_features;
  656. netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  657. }
  658. /**
  659. * link_status_event_handler - link event handler
  660. * @handle: nic device for the handler
  661. * @buf_in: input buffer
  662. * @in_size: input size
  663. * @buf_in: output buffer
  664. * @out_size: returned output size
  665. *
  666. * Return 0 - Success, negative - Failure
  667. **/
  668. static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
  669. void *buf_out, u16 *out_size)
  670. {
  671. struct hinic_port_link_status *link_status, *ret_link_status;
  672. struct hinic_dev *nic_dev = handle;
  673. link_status = buf_in;
  674. if (link_status->link == HINIC_LINK_STATE_UP) {
  675. down(&nic_dev->mgmt_lock);
  676. nic_dev->flags |= HINIC_LINK_UP;
  677. if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
  678. (HINIC_LINK_UP | HINIC_INTF_UP)) {
  679. netif_carrier_on(nic_dev->netdev);
  680. netif_tx_wake_all_queues(nic_dev->netdev);
  681. }
  682. up(&nic_dev->mgmt_lock);
  683. netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
  684. } else {
  685. down(&nic_dev->mgmt_lock);
  686. nic_dev->flags &= ~HINIC_LINK_UP;
  687. netif_carrier_off(nic_dev->netdev);
  688. netif_tx_disable(nic_dev->netdev);
  689. up(&nic_dev->mgmt_lock);
  690. netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
  691. }
  692. ret_link_status = buf_out;
  693. ret_link_status->status = 0;
  694. *out_size = sizeof(*ret_link_status);
  695. }
  696. /**
  697. * nic_dev_init - Initialize the NIC device
  698. * @pdev: the NIC pci device
  699. *
  700. * Return 0 - Success, negative - Failure
  701. **/
  702. static int nic_dev_init(struct pci_dev *pdev)
  703. {
  704. struct hinic_rx_mode_work *rx_mode_work;
  705. struct hinic_txq_stats *tx_stats;
  706. struct hinic_rxq_stats *rx_stats;
  707. struct hinic_dev *nic_dev;
  708. struct net_device *netdev;
  709. struct hinic_hwdev *hwdev;
  710. int err, num_qps;
  711. hwdev = hinic_init_hwdev(pdev);
  712. if (IS_ERR(hwdev)) {
  713. dev_err(&pdev->dev, "Failed to initialize HW device\n");
  714. return PTR_ERR(hwdev);
  715. }
  716. num_qps = hinic_hwdev_num_qps(hwdev);
  717. if (num_qps <= 0) {
  718. dev_err(&pdev->dev, "Invalid number of QPS\n");
  719. err = -EINVAL;
  720. goto err_num_qps;
  721. }
  722. netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
  723. if (!netdev) {
  724. dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
  725. err = -ENOMEM;
  726. goto err_alloc_etherdev;
  727. }
  728. netdev->netdev_ops = &hinic_netdev_ops;
  729. netdev->ethtool_ops = &hinic_ethtool_ops;
  730. netdev->max_mtu = ETH_MAX_MTU;
  731. nic_dev = netdev_priv(netdev);
  732. nic_dev->netdev = netdev;
  733. nic_dev->hwdev = hwdev;
  734. nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
  735. nic_dev->flags = 0;
  736. nic_dev->txqs = NULL;
  737. nic_dev->rxqs = NULL;
  738. nic_dev->tx_weight = tx_weight;
  739. nic_dev->rx_weight = rx_weight;
  740. sema_init(&nic_dev->mgmt_lock, 1);
  741. tx_stats = &nic_dev->tx_stats;
  742. rx_stats = &nic_dev->rx_stats;
  743. u64_stats_init(&tx_stats->syncp);
  744. u64_stats_init(&rx_stats->syncp);
  745. nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
  746. VLAN_BITMAP_SIZE(nic_dev),
  747. GFP_KERNEL);
  748. if (!nic_dev->vlan_bitmap) {
  749. err = -ENOMEM;
  750. goto err_vlan_bitmap;
  751. }
  752. nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
  753. if (!nic_dev->workq) {
  754. err = -ENOMEM;
  755. goto err_workq;
  756. }
  757. pci_set_drvdata(pdev, netdev);
  758. err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
  759. if (err)
  760. dev_warn(&pdev->dev, "Failed to get mac address\n");
  761. err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
  762. if (err) {
  763. dev_err(&pdev->dev, "Failed to add mac\n");
  764. goto err_add_mac;
  765. }
  766. err = hinic_port_set_mtu(nic_dev, netdev->mtu);
  767. if (err) {
  768. dev_err(&pdev->dev, "Failed to set mtu\n");
  769. goto err_set_mtu;
  770. }
  771. rx_mode_work = &nic_dev->rx_mode_work;
  772. INIT_WORK(&rx_mode_work->work, set_rx_mode);
  773. netdev_features_init(netdev);
  774. netif_carrier_off(netdev);
  775. hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
  776. nic_dev, link_status_event_handler);
  777. err = register_netdev(netdev);
  778. if (err) {
  779. dev_err(&pdev->dev, "Failed to register netdev\n");
  780. goto err_reg_netdev;
  781. }
  782. return 0;
  783. err_reg_netdev:
  784. hinic_hwdev_cb_unregister(nic_dev->hwdev,
  785. HINIC_MGMT_MSG_CMD_LINK_STATUS);
  786. cancel_work_sync(&rx_mode_work->work);
  787. err_set_mtu:
  788. err_add_mac:
  789. pci_set_drvdata(pdev, NULL);
  790. destroy_workqueue(nic_dev->workq);
  791. err_workq:
  792. err_vlan_bitmap:
  793. free_netdev(netdev);
  794. err_alloc_etherdev:
  795. err_num_qps:
  796. hinic_free_hwdev(hwdev);
  797. return err;
  798. }
  799. static int hinic_probe(struct pci_dev *pdev,
  800. const struct pci_device_id *id)
  801. {
  802. int err = pci_enable_device(pdev);
  803. if (err) {
  804. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  805. return err;
  806. }
  807. err = pci_request_regions(pdev, HINIC_DRV_NAME);
  808. if (err) {
  809. dev_err(&pdev->dev, "Failed to request PCI regions\n");
  810. goto err_pci_regions;
  811. }
  812. pci_set_master(pdev);
  813. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  814. if (err) {
  815. dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
  816. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  817. if (err) {
  818. dev_err(&pdev->dev, "Failed to set DMA mask\n");
  819. goto err_dma_mask;
  820. }
  821. }
  822. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  823. if (err) {
  824. dev_warn(&pdev->dev,
  825. "Couldn't set 64-bit consistent DMA mask\n");
  826. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  827. if (err) {
  828. dev_err(&pdev->dev,
  829. "Failed to set consistent DMA mask\n");
  830. goto err_dma_consistent_mask;
  831. }
  832. }
  833. err = nic_dev_init(pdev);
  834. if (err) {
  835. dev_err(&pdev->dev, "Failed to initialize NIC device\n");
  836. goto err_nic_dev_init;
  837. }
  838. dev_info(&pdev->dev, "HiNIC driver - probed\n");
  839. return 0;
  840. err_nic_dev_init:
  841. err_dma_consistent_mask:
  842. err_dma_mask:
  843. pci_release_regions(pdev);
  844. err_pci_regions:
  845. pci_disable_device(pdev);
  846. return err;
  847. }
  848. static void hinic_remove(struct pci_dev *pdev)
  849. {
  850. struct net_device *netdev = pci_get_drvdata(pdev);
  851. struct hinic_dev *nic_dev = netdev_priv(netdev);
  852. struct hinic_rx_mode_work *rx_mode_work;
  853. unregister_netdev(netdev);
  854. hinic_hwdev_cb_unregister(nic_dev->hwdev,
  855. HINIC_MGMT_MSG_CMD_LINK_STATUS);
  856. rx_mode_work = &nic_dev->rx_mode_work;
  857. cancel_work_sync(&rx_mode_work->work);
  858. pci_set_drvdata(pdev, NULL);
  859. destroy_workqueue(nic_dev->workq);
  860. hinic_free_hwdev(nic_dev->hwdev);
  861. free_netdev(netdev);
  862. pci_release_regions(pdev);
  863. pci_disable_device(pdev);
  864. dev_info(&pdev->dev, "HiNIC driver - removed\n");
  865. }
  866. static const struct pci_device_id hinic_pci_table[] = {
  867. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
  868. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_25GE), 0},
  869. { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
  870. { 0, 0}
  871. };
  872. MODULE_DEVICE_TABLE(pci, hinic_pci_table);
  873. static struct pci_driver hinic_driver = {
  874. .name = HINIC_DRV_NAME,
  875. .id_table = hinic_pci_table,
  876. .probe = hinic_probe,
  877. .remove = hinic_remove,
  878. };
  879. module_pci_driver(hinic_driver);