fbnic_netdev.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) Meta Platforms, Inc. and affiliates. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/ipv6.h>
  5. #include <linux/types.h>
  6. #include <net/netdev_queues.h>
  7. #include "fbnic.h"
  8. #include "fbnic_netdev.h"
  9. #include "fbnic_txrx.h"
  10. int __fbnic_open(struct fbnic_net *fbn)
  11. {
  12. struct fbnic_dev *fbd = fbn->fbd;
  13. int err;
  14. err = fbnic_alloc_napi_vectors(fbn);
  15. if (err)
  16. return err;
  17. err = fbnic_alloc_resources(fbn);
  18. if (err)
  19. goto free_napi_vectors;
  20. err = fbnic_set_netif_queues(fbn);
  21. if (err)
  22. goto free_resources;
  23. /* Send ownership message and flush to verify FW has seen it */
  24. err = fbnic_fw_xmit_ownership_msg(fbd, true);
  25. if (err) {
  26. dev_warn(fbd->dev,
  27. "Error %d sending host ownership message to the firmware\n",
  28. err);
  29. goto err_reset_queues;
  30. }
  31. err = fbnic_time_start(fbn);
  32. if (err)
  33. goto release_ownership;
  34. err = fbnic_fw_init_heartbeat(fbd, false);
  35. if (err)
  36. goto time_stop;
  37. err = fbnic_mac_request_irq(fbd);
  38. if (err)
  39. goto time_stop;
  40. /* Pull the BMC config and initialize the RPC */
  41. fbnic_bmc_rpc_init(fbd);
  42. fbnic_rss_reinit(fbd, fbn);
  43. phylink_resume(fbn->phylink);
  44. return 0;
  45. time_stop:
  46. fbnic_time_stop(fbn);
  47. release_ownership:
  48. fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
  49. err_reset_queues:
  50. fbnic_reset_netif_queues(fbn);
  51. free_resources:
  52. fbnic_free_resources(fbn);
  53. free_napi_vectors:
  54. fbnic_free_napi_vectors(fbn);
  55. return err;
  56. }
  57. static int fbnic_open(struct net_device *netdev)
  58. {
  59. struct fbnic_net *fbn = netdev_priv(netdev);
  60. int err;
  61. fbnic_napi_name_irqs(fbn->fbd);
  62. err = __fbnic_open(fbn);
  63. if (!err)
  64. fbnic_up(fbn);
  65. return err;
  66. }
  67. static int fbnic_stop(struct net_device *netdev)
  68. {
  69. struct fbnic_net *fbn = netdev_priv(netdev);
  70. fbnic_mac_free_irq(fbn->fbd);
  71. phylink_suspend(fbn->phylink, fbnic_bmc_present(fbn->fbd));
  72. fbnic_down(fbn);
  73. fbnic_time_stop(fbn);
  74. fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
  75. fbnic_reset_netif_queues(fbn);
  76. fbnic_free_resources(fbn);
  77. fbnic_free_napi_vectors(fbn);
  78. return 0;
  79. }
  80. static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr)
  81. {
  82. struct fbnic_net *fbn = netdev_priv(netdev);
  83. struct fbnic_mac_addr *avail_addr;
  84. if (WARN_ON(!is_valid_ether_addr(addr)))
  85. return -EADDRNOTAVAIL;
  86. avail_addr = __fbnic_uc_sync(fbn->fbd, addr);
  87. if (!avail_addr)
  88. return -ENOSPC;
  89. /* Add type flag indicating this address is in use by the host */
  90. set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam);
  91. return 0;
  92. }
  93. static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr)
  94. {
  95. struct fbnic_net *fbn = netdev_priv(netdev);
  96. struct fbnic_dev *fbd = fbn->fbd;
  97. int i, ret;
  98. /* Scan from middle of list to bottom, filling bottom up.
  99. * Skip the first entry which is reserved for dev_addr and
  100. * leave the last entry to use for promiscuous filtering.
  101. */
  102. for (i = fbd->mac_addr_boundary, ret = -ENOENT;
  103. i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) {
  104. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
  105. if (!ether_addr_equal(mac_addr->value.addr8, addr))
  106. continue;
  107. ret = __fbnic_uc_unsync(mac_addr);
  108. }
  109. return ret;
  110. }
  111. static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr)
  112. {
  113. struct fbnic_net *fbn = netdev_priv(netdev);
  114. struct fbnic_mac_addr *avail_addr;
  115. if (WARN_ON(!is_multicast_ether_addr(addr)))
  116. return -EADDRNOTAVAIL;
  117. avail_addr = __fbnic_mc_sync(fbn->fbd, addr);
  118. if (!avail_addr)
  119. return -ENOSPC;
  120. /* Add type flag indicating this address is in use by the host */
  121. set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam);
  122. return 0;
  123. }
  124. static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr)
  125. {
  126. struct fbnic_net *fbn = netdev_priv(netdev);
  127. struct fbnic_dev *fbd = fbn->fbd;
  128. int i, ret;
  129. /* Scan from middle of list to top, filling top down.
  130. * Skip over the address reserved for the BMC MAC and
  131. * exclude index 0 as that belongs to the broadcast address
  132. */
  133. for (i = fbd->mac_addr_boundary, ret = -ENOENT;
  134. --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) {
  135. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
  136. if (!ether_addr_equal(mac_addr->value.addr8, addr))
  137. continue;
  138. ret = __fbnic_mc_unsync(mac_addr);
  139. }
  140. return ret;
  141. }
  142. void __fbnic_set_rx_mode(struct fbnic_dev *fbd)
  143. {
  144. bool uc_promisc = false, mc_promisc = false;
  145. struct net_device *netdev = fbd->netdev;
  146. struct fbnic_mac_addr *mac_addr;
  147. int err;
  148. /* Populate host address from dev_addr */
  149. mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX];
  150. if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) ||
  151. mac_addr->state != FBNIC_TCAM_S_VALID) {
  152. ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr);
  153. mac_addr->state = FBNIC_TCAM_S_UPDATE;
  154. set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam);
  155. }
  156. /* Populate broadcast address if broadcast is enabled */
  157. mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
  158. if (netdev->flags & IFF_BROADCAST) {
  159. if (!is_broadcast_ether_addr(mac_addr->value.addr8) ||
  160. mac_addr->state != FBNIC_TCAM_S_VALID) {
  161. eth_broadcast_addr(mac_addr->value.addr8);
  162. mac_addr->state = FBNIC_TCAM_S_ADD;
  163. }
  164. set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam);
  165. } else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
  166. __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST);
  167. }
  168. /* Synchronize unicast and multicast address lists */
  169. err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync);
  170. if (err == -ENOSPC)
  171. uc_promisc = true;
  172. err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync);
  173. if (err == -ENOSPC)
  174. mc_promisc = true;
  175. uc_promisc |= !!(netdev->flags & IFF_PROMISC);
  176. mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc;
  177. /* Update the promiscuous rules */
  178. fbnic_promisc_sync(fbd, uc_promisc, mc_promisc);
  179. /* Add rules for BMC all multicast if it is enabled */
  180. fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc);
  181. /* Sift out any unshared BMC rules and place them in BMC only section */
  182. fbnic_sift_macda(fbd);
  183. /* Write updates to hardware */
  184. fbnic_write_rules(fbd);
  185. fbnic_write_macda(fbd);
  186. fbnic_write_tce_tcam(fbd);
  187. }
  188. static void fbnic_set_rx_mode(struct net_device *netdev)
  189. {
  190. struct fbnic_net *fbn = netdev_priv(netdev);
  191. struct fbnic_dev *fbd = fbn->fbd;
  192. /* No need to update the hardware if we are not running */
  193. if (netif_running(netdev))
  194. __fbnic_set_rx_mode(fbd);
  195. }
  196. static int fbnic_set_mac(struct net_device *netdev, void *p)
  197. {
  198. struct sockaddr *addr = p;
  199. if (!is_valid_ether_addr(addr->sa_data))
  200. return -EADDRNOTAVAIL;
  201. eth_hw_addr_set(netdev, addr->sa_data);
  202. fbnic_set_rx_mode(netdev);
  203. return 0;
  204. }
  205. static int fbnic_change_mtu(struct net_device *dev, int new_mtu)
  206. {
  207. struct fbnic_net *fbn = netdev_priv(dev);
  208. if (fbnic_check_split_frames(fbn->xdp_prog, new_mtu, fbn->hds_thresh)) {
  209. dev_err(&dev->dev,
  210. "MTU %d is larger than HDS threshold %d in XDP mode\n",
  211. new_mtu, fbn->hds_thresh);
  212. return -EINVAL;
  213. }
  214. WRITE_ONCE(dev->mtu, new_mtu);
  215. return 0;
  216. }
  217. void fbnic_clear_rx_mode(struct fbnic_dev *fbd)
  218. {
  219. struct net_device *netdev = fbd->netdev;
  220. int idx;
  221. for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
  222. struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
  223. if (mac_addr->state != FBNIC_TCAM_S_VALID)
  224. continue;
  225. bitmap_clear(mac_addr->act_tcam,
  226. FBNIC_MAC_ADDR_T_HOST_START,
  227. FBNIC_MAC_ADDR_T_HOST_LEN);
  228. if (bitmap_empty(mac_addr->act_tcam,
  229. FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
  230. mac_addr->state = FBNIC_TCAM_S_DELETE;
  231. }
  232. /* Write updates to hardware */
  233. fbnic_write_macda(fbd);
  234. __dev_uc_unsync(netdev, NULL);
  235. __dev_mc_unsync(netdev, NULL);
  236. }
  237. static int fbnic_hwtstamp_get(struct net_device *netdev,
  238. struct kernel_hwtstamp_config *config)
  239. {
  240. struct fbnic_net *fbn = netdev_priv(netdev);
  241. *config = fbn->hwtstamp_config;
  242. return 0;
  243. }
  244. static int fbnic_hwtstamp_set(struct net_device *netdev,
  245. struct kernel_hwtstamp_config *config,
  246. struct netlink_ext_ack *extack)
  247. {
  248. struct fbnic_net *fbn = netdev_priv(netdev);
  249. int old_rx_filter;
  250. if (config->source != HWTSTAMP_SOURCE_NETDEV)
  251. return -EOPNOTSUPP;
  252. if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config))
  253. return 0;
  254. /* Upscale the filters */
  255. switch (config->rx_filter) {
  256. case HWTSTAMP_FILTER_NONE:
  257. case HWTSTAMP_FILTER_ALL:
  258. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  259. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  260. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  261. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  262. break;
  263. case HWTSTAMP_FILTER_NTP_ALL:
  264. config->rx_filter = HWTSTAMP_FILTER_ALL;
  265. break;
  266. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  267. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  268. config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  269. break;
  270. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  271. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  272. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  273. break;
  274. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  275. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  276. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  277. break;
  278. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  279. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  280. config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  281. break;
  282. default:
  283. return -ERANGE;
  284. }
  285. /* Configure */
  286. old_rx_filter = fbn->hwtstamp_config.rx_filter;
  287. memcpy(&fbn->hwtstamp_config, config, sizeof(*config));
  288. if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) {
  289. fbnic_rss_reinit(fbn->fbd, fbn);
  290. fbnic_write_rules(fbn->fbd);
  291. }
  292. /* Save / report back filter configuration
  293. * Note that our filter configuration is inexact. Instead of
  294. * filtering for a specific UDP port or L2 Ethertype we are
  295. * filtering in all UDP or all non-IP packets for timestamping. So
  296. * if anything other than FILTER_ALL is requested we report
  297. * FILTER_SOME indicating that we will be timestamping a few
  298. * additional packets.
  299. */
  300. if (config->rx_filter > HWTSTAMP_FILTER_ALL)
  301. config->rx_filter = HWTSTAMP_FILTER_SOME;
  302. return 0;
  303. }
  304. static void fbnic_get_stats64(struct net_device *dev,
  305. struct rtnl_link_stats64 *stats64)
  306. {
  307. u64 rx_bytes, rx_packets, rx_dropped = 0, rx_errors = 0;
  308. u64 rx_over = 0, rx_missed = 0, rx_length = 0;
  309. u64 tx_bytes, tx_packets, tx_dropped = 0;
  310. struct fbnic_net *fbn = netdev_priv(dev);
  311. struct fbnic_dev *fbd = fbn->fbd;
  312. struct fbnic_queue_stats *stats;
  313. unsigned int start, i;
  314. fbnic_get_hw_stats(fbd);
  315. stats = &fbn->tx_stats;
  316. tx_bytes = stats->bytes;
  317. tx_packets = stats->packets;
  318. tx_dropped = stats->dropped;
  319. /* Record drops from Tx HW Datapath */
  320. spin_lock(&fbd->hw_stats.lock);
  321. tx_dropped += fbd->hw_stats.tmi.drop.frames.value +
  322. fbd->hw_stats.tti.cm_drop.frames.value +
  323. fbd->hw_stats.tti.frame_drop.frames.value +
  324. fbd->hw_stats.tti.tbi_drop.frames.value;
  325. spin_unlock(&fbd->hw_stats.lock);
  326. stats64->tx_bytes = tx_bytes;
  327. stats64->tx_packets = tx_packets;
  328. stats64->tx_dropped = tx_dropped;
  329. for (i = 0; i < fbn->num_tx_queues; i++) {
  330. struct fbnic_ring *txr = fbn->tx[i];
  331. if (!txr)
  332. continue;
  333. stats = &txr->stats;
  334. do {
  335. start = u64_stats_fetch_begin(&stats->syncp);
  336. tx_bytes = stats->bytes;
  337. tx_packets = stats->packets;
  338. tx_dropped = stats->dropped;
  339. } while (u64_stats_fetch_retry(&stats->syncp, start));
  340. stats64->tx_bytes += tx_bytes;
  341. stats64->tx_packets += tx_packets;
  342. stats64->tx_dropped += tx_dropped;
  343. }
  344. stats = &fbn->rx_stats;
  345. rx_bytes = stats->bytes;
  346. rx_packets = stats->packets;
  347. rx_dropped = stats->dropped;
  348. spin_lock(&fbd->hw_stats.lock);
  349. /* Record drops for the host FIFOs.
  350. * 4: network to Host, 6: BMC to Host
  351. * Exclude the BMC and MC FIFOs as those stats may contain drops
  352. * due to unrelated items such as TCAM misses. They are still
  353. * accessible through the ethtool stats.
  354. */
  355. i = FBNIC_RXB_FIFO_HOST;
  356. rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
  357. i = FBNIC_RXB_FIFO_BMC_TO_HOST;
  358. rx_missed += fbd->hw_stats.rxb.fifo[i].drop.frames.value;
  359. for (i = 0; i < fbd->max_num_queues; i++) {
  360. /* Report packets dropped due to CQ/BDQ being full/empty */
  361. rx_over += fbd->hw_stats.hw_q[i].rde_pkt_cq_drop.value;
  362. rx_over += fbd->hw_stats.hw_q[i].rde_pkt_bdq_drop.value;
  363. /* Report packets with errors */
  364. rx_errors += fbd->hw_stats.hw_q[i].rde_pkt_err.value;
  365. }
  366. spin_unlock(&fbd->hw_stats.lock);
  367. stats64->rx_bytes = rx_bytes;
  368. stats64->rx_packets = rx_packets;
  369. stats64->rx_dropped = rx_dropped;
  370. stats64->rx_over_errors = rx_over;
  371. stats64->rx_errors = rx_errors;
  372. stats64->rx_missed_errors = rx_missed;
  373. for (i = 0; i < fbn->num_rx_queues; i++) {
  374. struct fbnic_ring *xdpr = fbn->tx[FBNIC_MAX_TXQS + i];
  375. struct fbnic_ring *rxr = fbn->rx[i];
  376. if (!rxr)
  377. continue;
  378. stats = &rxr->stats;
  379. do {
  380. start = u64_stats_fetch_begin(&stats->syncp);
  381. rx_bytes = stats->bytes;
  382. rx_packets = stats->packets;
  383. rx_dropped = stats->dropped;
  384. rx_length = stats->rx.length_errors;
  385. } while (u64_stats_fetch_retry(&stats->syncp, start));
  386. stats64->rx_bytes += rx_bytes;
  387. stats64->rx_packets += rx_packets;
  388. stats64->rx_dropped += rx_dropped;
  389. stats64->rx_errors += rx_length;
  390. stats64->rx_length_errors += rx_length;
  391. if (!xdpr)
  392. continue;
  393. stats = &xdpr->stats;
  394. do {
  395. start = u64_stats_fetch_begin(&stats->syncp);
  396. tx_bytes = stats->bytes;
  397. tx_packets = stats->packets;
  398. tx_dropped = stats->dropped;
  399. } while (u64_stats_fetch_retry(&stats->syncp, start));
  400. stats64->tx_bytes += tx_bytes;
  401. stats64->tx_packets += tx_packets;
  402. stats64->tx_dropped += tx_dropped;
  403. }
  404. }
  405. bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu,
  406. u32 hds_thresh)
  407. {
  408. if (!prog)
  409. return false;
  410. if (prog->aux->xdp_has_frags)
  411. return false;
  412. return mtu + ETH_HLEN > hds_thresh;
  413. }
  414. static int fbnic_bpf(struct net_device *netdev, struct netdev_bpf *bpf)
  415. {
  416. struct bpf_prog *prog = bpf->prog, *prev_prog;
  417. struct fbnic_net *fbn = netdev_priv(netdev);
  418. if (bpf->command != XDP_SETUP_PROG)
  419. return -EINVAL;
  420. if (fbnic_check_split_frames(prog, netdev->mtu,
  421. fbn->hds_thresh)) {
  422. NL_SET_ERR_MSG_MOD(bpf->extack,
  423. "MTU too high, or HDS threshold is too low for single buffer XDP");
  424. return -EOPNOTSUPP;
  425. }
  426. prev_prog = xchg(&fbn->xdp_prog, prog);
  427. if (prev_prog)
  428. bpf_prog_put(prev_prog);
  429. return 0;
  430. }
  431. static const struct net_device_ops fbnic_netdev_ops = {
  432. .ndo_open = fbnic_open,
  433. .ndo_stop = fbnic_stop,
  434. .ndo_validate_addr = eth_validate_addr,
  435. .ndo_start_xmit = fbnic_xmit_frame,
  436. .ndo_features_check = fbnic_features_check,
  437. .ndo_set_mac_address = fbnic_set_mac,
  438. .ndo_change_mtu = fbnic_change_mtu,
  439. .ndo_set_rx_mode = fbnic_set_rx_mode,
  440. .ndo_get_stats64 = fbnic_get_stats64,
  441. .ndo_bpf = fbnic_bpf,
  442. .ndo_hwtstamp_get = fbnic_hwtstamp_get,
  443. .ndo_hwtstamp_set = fbnic_hwtstamp_set,
  444. };
  445. static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx,
  446. struct netdev_queue_stats_rx *rx)
  447. {
  448. u64 bytes, packets, alloc_fail, alloc_fail_bdq;
  449. struct fbnic_net *fbn = netdev_priv(dev);
  450. struct fbnic_ring *rxr = fbn->rx[idx];
  451. struct fbnic_dev *fbd = fbn->fbd;
  452. struct fbnic_queue_stats *stats;
  453. u64 csum_complete, csum_none;
  454. struct fbnic_q_triad *qt;
  455. unsigned int start;
  456. if (!rxr)
  457. return;
  458. /* fbn->rx points to completion queues */
  459. qt = container_of(rxr, struct fbnic_q_triad, cmpl);
  460. stats = &rxr->stats;
  461. do {
  462. start = u64_stats_fetch_begin(&stats->syncp);
  463. bytes = stats->bytes;
  464. packets = stats->packets;
  465. alloc_fail = stats->rx.alloc_failed;
  466. csum_complete = stats->rx.csum_complete;
  467. csum_none = stats->rx.csum_none;
  468. } while (u64_stats_fetch_retry(&stats->syncp, start));
  469. stats = &qt->sub0.stats;
  470. do {
  471. start = u64_stats_fetch_begin(&stats->syncp);
  472. alloc_fail_bdq = stats->bdq.alloc_failed;
  473. } while (u64_stats_fetch_retry(&stats->syncp, start));
  474. alloc_fail += alloc_fail_bdq;
  475. stats = &qt->sub1.stats;
  476. do {
  477. start = u64_stats_fetch_begin(&stats->syncp);
  478. alloc_fail_bdq = stats->bdq.alloc_failed;
  479. } while (u64_stats_fetch_retry(&stats->syncp, start));
  480. alloc_fail += alloc_fail_bdq;
  481. rx->bytes = bytes;
  482. rx->packets = packets;
  483. rx->alloc_fail = alloc_fail;
  484. rx->csum_complete = csum_complete;
  485. rx->csum_none = csum_none;
  486. fbnic_get_hw_q_stats(fbd, fbd->hw_stats.hw_q);
  487. spin_lock(&fbd->hw_stats.lock);
  488. rx->hw_drop_overruns = fbd->hw_stats.hw_q[idx].rde_pkt_cq_drop.value +
  489. fbd->hw_stats.hw_q[idx].rde_pkt_bdq_drop.value;
  490. rx->hw_drops = fbd->hw_stats.hw_q[idx].rde_pkt_err.value +
  491. rx->hw_drop_overruns;
  492. spin_unlock(&fbd->hw_stats.lock);
  493. }
  494. static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx,
  495. struct netdev_queue_stats_tx *tx)
  496. {
  497. struct fbnic_net *fbn = netdev_priv(dev);
  498. struct fbnic_ring *txr = fbn->tx[idx];
  499. struct fbnic_queue_stats *stats;
  500. u64 stop, wake, csum, lso;
  501. struct fbnic_ring *xdpr;
  502. unsigned int start;
  503. u64 bytes, packets;
  504. if (!txr)
  505. return;
  506. stats = &txr->stats;
  507. do {
  508. start = u64_stats_fetch_begin(&stats->syncp);
  509. bytes = stats->bytes;
  510. packets = stats->packets;
  511. csum = stats->twq.csum_partial;
  512. lso = stats->twq.lso;
  513. stop = stats->twq.stop;
  514. wake = stats->twq.wake;
  515. } while (u64_stats_fetch_retry(&stats->syncp, start));
  516. tx->bytes = bytes;
  517. tx->packets = packets;
  518. tx->needs_csum = csum + lso;
  519. tx->hw_gso_wire_packets = lso;
  520. tx->stop = stop;
  521. tx->wake = wake;
  522. xdpr = fbn->tx[FBNIC_MAX_TXQS + idx];
  523. if (xdpr) {
  524. stats = &xdpr->stats;
  525. do {
  526. start = u64_stats_fetch_begin(&stats->syncp);
  527. bytes = stats->bytes;
  528. packets = stats->packets;
  529. } while (u64_stats_fetch_retry(&stats->syncp, start));
  530. tx->bytes += bytes;
  531. tx->packets += packets;
  532. }
  533. }
  534. static void fbnic_get_base_stats(struct net_device *dev,
  535. struct netdev_queue_stats_rx *rx,
  536. struct netdev_queue_stats_tx *tx)
  537. {
  538. struct fbnic_net *fbn = netdev_priv(dev);
  539. tx->bytes = fbn->tx_stats.bytes;
  540. tx->packets = fbn->tx_stats.packets;
  541. tx->needs_csum = fbn->tx_stats.twq.csum_partial + fbn->tx_stats.twq.lso;
  542. tx->hw_gso_wire_packets = fbn->tx_stats.twq.lso;
  543. tx->stop = fbn->tx_stats.twq.stop;
  544. tx->wake = fbn->tx_stats.twq.wake;
  545. rx->bytes = fbn->rx_stats.bytes;
  546. rx->packets = fbn->rx_stats.packets;
  547. rx->alloc_fail = fbn->rx_stats.rx.alloc_failed +
  548. fbn->bdq_stats.bdq.alloc_failed;
  549. rx->csum_complete = fbn->rx_stats.rx.csum_complete;
  550. rx->csum_none = fbn->rx_stats.rx.csum_none;
  551. }
  552. static const struct netdev_stat_ops fbnic_stat_ops = {
  553. .get_queue_stats_rx = fbnic_get_queue_stats_rx,
  554. .get_queue_stats_tx = fbnic_get_queue_stats_tx,
  555. .get_base_stats = fbnic_get_base_stats,
  556. };
  557. void fbnic_reset_queues(struct fbnic_net *fbn,
  558. unsigned int tx, unsigned int rx)
  559. {
  560. struct fbnic_dev *fbd = fbn->fbd;
  561. unsigned int max_napis;
  562. max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
  563. tx = min(tx, max_napis);
  564. fbn->num_tx_queues = tx;
  565. rx = min(rx, max_napis);
  566. fbn->num_rx_queues = rx;
  567. fbn->num_napi = max(tx, rx);
  568. }
  569. /**
  570. * fbnic_netdev_free - Free the netdev associate with fbnic
  571. * @fbd: Driver specific structure to free netdev from
  572. *
  573. * Allocate and initialize the netdev and netdev private structure. Bind
  574. * together the hardware, netdev, and pci data structures.
  575. **/
  576. void fbnic_netdev_free(struct fbnic_dev *fbd)
  577. {
  578. fbnic_phylink_destroy(fbd->netdev);
  579. free_netdev(fbd->netdev);
  580. fbd->netdev = NULL;
  581. }
  582. /**
  583. * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic
  584. * @fbd: Driver specific structure to associate netdev with
  585. *
  586. * Allocate and initialize the netdev and netdev private structure. Bind
  587. * together the hardware, netdev, and pci data structures.
  588. *
  589. * Return: Pointer to net_device on success, NULL on failure
  590. **/
  591. struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
  592. {
  593. struct net_device *netdev;
  594. struct fbnic_net *fbn;
  595. int default_queues;
  596. netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS);
  597. if (!netdev)
  598. return NULL;
  599. SET_NETDEV_DEV(netdev, fbd->dev);
  600. fbd->netdev = netdev;
  601. netdev->netdev_ops = &fbnic_netdev_ops;
  602. netdev->stat_ops = &fbnic_stat_ops;
  603. netdev->queue_mgmt_ops = &fbnic_queue_mgmt_ops;
  604. netdev->netmem_tx = true;
  605. fbnic_set_ethtool_ops(netdev);
  606. fbn = netdev_priv(netdev);
  607. fbn->netdev = netdev;
  608. fbn->fbd = fbd;
  609. fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT;
  610. fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT;
  611. fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT;
  612. fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT;
  613. fbn->tx_usecs = FBNIC_TX_USECS_DEFAULT;
  614. fbn->rx_usecs = FBNIC_RX_USECS_DEFAULT;
  615. fbn->rx_max_frames = FBNIC_RX_FRAMES_DEFAULT;
  616. /* Initialize the hds_thresh */
  617. netdev->cfg->hds_thresh = FBNIC_HDS_THRESH_DEFAULT;
  618. fbn->hds_thresh = FBNIC_HDS_THRESH_DEFAULT;
  619. default_queues = netif_get_num_default_rss_queues();
  620. if (default_queues > fbd->max_num_queues)
  621. default_queues = fbd->max_num_queues;
  622. fbnic_reset_queues(fbn, default_queues, default_queues);
  623. fbnic_reset_indir_tbl(fbn);
  624. fbnic_rss_key_fill(fbn->rss_key);
  625. fbnic_rss_init_en_mask(fbn);
  626. netdev->priv_flags |= IFF_UNICAST_FLT;
  627. netdev->gso_partial_features =
  628. NETIF_F_GSO_GRE |
  629. NETIF_F_GSO_GRE_CSUM |
  630. NETIF_F_GSO_IPXIP4 |
  631. NETIF_F_GSO_UDP_TUNNEL |
  632. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  633. netdev->features |=
  634. netdev->gso_partial_features |
  635. FBNIC_TUN_GSO_FEATURES |
  636. NETIF_F_RXHASH |
  637. NETIF_F_SG |
  638. NETIF_F_HW_CSUM |
  639. NETIF_F_RXCSUM |
  640. NETIF_F_TSO |
  641. NETIF_F_TSO_ECN |
  642. NETIF_F_TSO6 |
  643. NETIF_F_GSO_PARTIAL |
  644. NETIF_F_GSO_UDP_L4;
  645. netdev->hw_features |= netdev->features;
  646. netdev->vlan_features |= netdev->features;
  647. netdev->hw_enc_features |= netdev->features;
  648. netdev->features |= NETIF_F_NTUPLE;
  649. netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_RX_SG;
  650. netdev->min_mtu = IPV6_MIN_MTU;
  651. netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
  652. /* TBD: This is workaround for BMC as phylink doesn't have support
  653. * for leavling the link enabled if a BMC is present.
  654. */
  655. netdev->ethtool->wol_enabled = true;
  656. netif_carrier_off(netdev);
  657. netif_tx_stop_all_queues(netdev);
  658. if (fbnic_phylink_create(netdev)) {
  659. fbnic_netdev_free(fbd);
  660. return NULL;
  661. }
  662. return netdev;
  663. }
  664. static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr)
  665. {
  666. addr[0] = (dsn >> 56) & 0xFF;
  667. addr[1] = (dsn >> 48) & 0xFF;
  668. addr[2] = (dsn >> 40) & 0xFF;
  669. addr[3] = (dsn >> 16) & 0xFF;
  670. addr[4] = (dsn >> 8) & 0xFF;
  671. addr[5] = dsn & 0xFF;
  672. return is_valid_ether_addr(addr) ? 0 : -EINVAL;
  673. }
  674. /**
  675. * fbnic_netdev_register - Initialize general software structures
  676. * @netdev: Netdev containing structure to initialize and register
  677. *
  678. * Initialize the MAC address for the netdev and register it.
  679. *
  680. * Return: 0 on success, negative on failure
  681. **/
  682. int fbnic_netdev_register(struct net_device *netdev)
  683. {
  684. struct fbnic_net *fbn = netdev_priv(netdev);
  685. struct fbnic_dev *fbd = fbn->fbd;
  686. u64 dsn = fbd->dsn;
  687. u8 addr[ETH_ALEN];
  688. int err;
  689. err = fbnic_dsn_to_mac_addr(dsn, addr);
  690. if (!err) {
  691. ether_addr_copy(netdev->perm_addr, addr);
  692. eth_hw_addr_set(netdev, addr);
  693. } else {
  694. /* A randomly assigned MAC address will cause provisioning
  695. * issues so instead just fail to spawn the netdev and
  696. * avoid any confusion.
  697. */
  698. dev_err(fbd->dev, "MAC addr %pM invalid\n", addr);
  699. return err;
  700. }
  701. return register_netdev(netdev);
  702. }
  703. void fbnic_netdev_unregister(struct net_device *netdev)
  704. {
  705. unregister_netdev(netdev);
  706. }