hinic_main.c 27 KB

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