hns_ae_adapt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Copyright (c) 2014-2015 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/etherdevice.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/spinlock.h>
  12. #include "hnae.h"
  13. #include "hns_dsaf_mac.h"
  14. #include "hns_dsaf_main.h"
  15. #include "hns_dsaf_ppe.h"
  16. #include "hns_dsaf_rcb.h"
  17. #define AE_NAME_PORT_ID_IDX 6
  18. #define ETH_STATIC_REG 1
  19. #define ETH_DUMP_REG 5
  20. #define ETH_GSTRING_LEN 32
  21. static struct hns_mac_cb *hns_get_mac_cb(struct hnae_handle *handle)
  22. {
  23. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  24. return vf_cb->mac_cb;
  25. }
  26. /**
  27. * hns_ae_map_eport_to_dport - translate enet port id to dsaf port id
  28. * @port_id: enet port id
  29. *: debug port 0-1, service port 2 -7 (dsaf mode only 2)
  30. * return: dsaf port id
  31. *: service ports 0 - 5, debug port 6-7
  32. **/
  33. static int hns_ae_map_eport_to_dport(u32 port_id)
  34. {
  35. int port_index;
  36. if (port_id < DSAF_DEBUG_NW_NUM)
  37. port_index = port_id + DSAF_SERVICE_PORT_NUM_PER_DSAF;
  38. else
  39. port_index = port_id - DSAF_DEBUG_NW_NUM;
  40. return port_index;
  41. }
  42. static struct dsaf_device *hns_ae_get_dsaf_dev(struct hnae_ae_dev *dev)
  43. {
  44. return container_of(dev, struct dsaf_device, ae_dev);
  45. }
  46. static struct hns_ppe_cb *hns_get_ppe_cb(struct hnae_handle *handle)
  47. {
  48. int ppe_index;
  49. int ppe_common_index;
  50. struct ppe_common_cb *ppe_comm;
  51. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  52. if (vf_cb->port_index < DSAF_SERVICE_PORT_NUM_PER_DSAF) {
  53. ppe_index = vf_cb->port_index;
  54. ppe_common_index = 0;
  55. } else {
  56. ppe_index = 0;
  57. ppe_common_index =
  58. vf_cb->port_index - DSAF_SERVICE_PORT_NUM_PER_DSAF + 1;
  59. }
  60. ppe_comm = vf_cb->dsaf_dev->ppe_common[ppe_common_index];
  61. return &ppe_comm->ppe_cb[ppe_index];
  62. }
  63. static int hns_ae_get_q_num_per_vf(
  64. struct dsaf_device *dsaf_dev, int port)
  65. {
  66. int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  67. return dsaf_dev->rcb_common[common_idx]->max_q_per_vf;
  68. }
  69. static int hns_ae_get_vf_num_per_port(
  70. struct dsaf_device *dsaf_dev, int port)
  71. {
  72. int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  73. return dsaf_dev->rcb_common[common_idx]->max_vfn;
  74. }
  75. static struct ring_pair_cb *hns_ae_get_base_ring_pair(
  76. struct dsaf_device *dsaf_dev, int port)
  77. {
  78. int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  79. struct rcb_common_cb *rcb_comm = dsaf_dev->rcb_common[common_idx];
  80. int q_num = rcb_comm->max_q_per_vf;
  81. int vf_num = rcb_comm->max_vfn;
  82. if (common_idx == HNS_DSAF_COMM_SERVICE_NW_IDX)
  83. return &rcb_comm->ring_pair_cb[port * q_num * vf_num];
  84. else
  85. return &rcb_comm->ring_pair_cb[0];
  86. }
  87. static struct ring_pair_cb *hns_ae_get_ring_pair(struct hnae_queue *q)
  88. {
  89. return container_of(q, struct ring_pair_cb, q);
  90. }
  91. struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
  92. u32 port_id)
  93. {
  94. int port_idx;
  95. int vfnum_per_port;
  96. int qnum_per_vf;
  97. int i;
  98. struct dsaf_device *dsaf_dev;
  99. struct hnae_handle *ae_handle;
  100. struct ring_pair_cb *ring_pair_cb;
  101. struct hnae_vf_cb *vf_cb;
  102. dsaf_dev = hns_ae_get_dsaf_dev(dev);
  103. port_idx = hns_ae_map_eport_to_dport(port_id);
  104. ring_pair_cb = hns_ae_get_base_ring_pair(dsaf_dev, port_idx);
  105. vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_idx);
  106. qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_idx);
  107. vf_cb = kzalloc(sizeof(*vf_cb) +
  108. qnum_per_vf * sizeof(struct hnae_queue *), GFP_KERNEL);
  109. if (unlikely(!vf_cb)) {
  110. dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
  111. ae_handle = ERR_PTR(-ENOMEM);
  112. goto handle_err;
  113. }
  114. ae_handle = &vf_cb->ae_handle;
  115. /* ae_handle Init */
  116. ae_handle->owner_dev = dsaf_dev->dev;
  117. ae_handle->dev = dev;
  118. ae_handle->q_num = qnum_per_vf;
  119. /* find ring pair, and set vf id*/
  120. for (ae_handle->vf_id = 0;
  121. ae_handle->vf_id < vfnum_per_port; ae_handle->vf_id++) {
  122. if (!ring_pair_cb->used_by_vf)
  123. break;
  124. ring_pair_cb += qnum_per_vf;
  125. }
  126. if (ae_handle->vf_id >= vfnum_per_port) {
  127. dev_err(dsaf_dev->dev, "malloc queue fail!\n");
  128. ae_handle = ERR_PTR(-EINVAL);
  129. goto vf_id_err;
  130. }
  131. ae_handle->qs = (struct hnae_queue **)(&ae_handle->qs + 1);
  132. for (i = 0; i < qnum_per_vf; i++) {
  133. ae_handle->qs[i] = &ring_pair_cb->q;
  134. ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];
  135. ae_handle->qs[i]->tx_ring.q = ae_handle->qs[i];
  136. ring_pair_cb->used_by_vf = 1;
  137. ring_pair_cb++;
  138. }
  139. vf_cb->dsaf_dev = dsaf_dev;
  140. vf_cb->port_index = port_idx;
  141. vf_cb->mac_cb = &dsaf_dev->mac_cb[port_idx];
  142. ae_handle->phy_if = vf_cb->mac_cb->phy_if;
  143. ae_handle->phy_node = vf_cb->mac_cb->phy_node;
  144. ae_handle->if_support = vf_cb->mac_cb->if_support;
  145. ae_handle->port_type = vf_cb->mac_cb->mac_type;
  146. ae_handle->dport_id = port_idx;
  147. return ae_handle;
  148. vf_id_err:
  149. kfree(vf_cb);
  150. handle_err:
  151. return ae_handle;
  152. }
  153. static void hns_ae_put_handle(struct hnae_handle *handle)
  154. {
  155. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  156. int i;
  157. vf_cb->mac_cb = NULL;
  158. kfree(vf_cb);
  159. for (i = 0; i < handle->q_num; i++)
  160. hns_ae_get_ring_pair(handle->qs[i])->used_by_vf = 0;
  161. }
  162. static void hns_ae_ring_enable_all(struct hnae_handle *handle, int val)
  163. {
  164. int q_num = handle->q_num;
  165. int i;
  166. for (i = 0; i < q_num; i++)
  167. hns_rcb_ring_enable_hw(handle->qs[i], val);
  168. }
  169. static void hns_ae_init_queue(struct hnae_queue *q)
  170. {
  171. struct ring_pair_cb *ring =
  172. container_of(q, struct ring_pair_cb, q);
  173. hns_rcb_init_hw(ring);
  174. }
  175. static void hns_ae_fini_queue(struct hnae_queue *q)
  176. {
  177. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(q->handle);
  178. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  179. hns_rcb_reset_ring_hw(q);
  180. }
  181. static int hns_ae_set_mac_address(struct hnae_handle *handle, void *p)
  182. {
  183. int ret;
  184. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  185. if (!p || !is_valid_ether_addr((const u8 *)p)) {
  186. dev_err(handle->owner_dev, "is not valid ether addr !\n");
  187. return -EADDRNOTAVAIL;
  188. }
  189. ret = hns_mac_change_vf_addr(mac_cb, handle->vf_id, p);
  190. if (ret != 0) {
  191. dev_err(handle->owner_dev,
  192. "set_mac_address fail, ret=%d!\n", ret);
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static int hns_ae_set_multicast_one(struct hnae_handle *handle, void *addr)
  198. {
  199. int ret;
  200. char *mac_addr = (char *)addr;
  201. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  202. assert(mac_cb);
  203. if (mac_cb->mac_type != HNAE_PORT_SERVICE)
  204. return 0;
  205. ret = hns_mac_set_multi(mac_cb, mac_cb->mac_id, mac_addr, true);
  206. if (ret) {
  207. dev_err(handle->owner_dev,
  208. "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
  209. mac_addr, mac_cb->mac_id, ret);
  210. return ret;
  211. }
  212. ret = hns_mac_set_multi(mac_cb, DSAF_BASE_INNER_PORT_NUM,
  213. mac_addr, true);
  214. if (ret)
  215. dev_err(handle->owner_dev,
  216. "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
  217. mac_addr, DSAF_BASE_INNER_PORT_NUM, ret);
  218. return ret;
  219. }
  220. static int hns_ae_set_mtu(struct hnae_handle *handle, int new_mtu)
  221. {
  222. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  223. return hns_mac_set_mtu(mac_cb, new_mtu);
  224. }
  225. static void hns_ae_set_tso_stats(struct hnae_handle *handle, int enable)
  226. {
  227. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  228. hns_ppe_set_tso_enable(ppe_cb, enable);
  229. }
  230. static int hns_ae_start(struct hnae_handle *handle)
  231. {
  232. int ret;
  233. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  234. ret = hns_mac_vm_config_bc_en(mac_cb, 0, true);
  235. if (ret)
  236. return ret;
  237. hns_ae_ring_enable_all(handle, 1);
  238. msleep(100);
  239. hns_mac_start(mac_cb);
  240. return 0;
  241. }
  242. void hns_ae_stop(struct hnae_handle *handle)
  243. {
  244. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  245. /* just clean tx fbd, neednot rx fbd*/
  246. hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_TX);
  247. msleep(20);
  248. hns_mac_stop(mac_cb);
  249. usleep_range(10000, 20000);
  250. hns_ae_ring_enable_all(handle, 0);
  251. (void)hns_mac_vm_config_bc_en(mac_cb, 0, false);
  252. }
  253. static void hns_ae_reset(struct hnae_handle *handle)
  254. {
  255. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  256. if (vf_cb->mac_cb->mac_type == HNAE_PORT_DEBUG) {
  257. u8 ppe_common_index =
  258. vf_cb->port_index - DSAF_SERVICE_PORT_NUM_PER_DSAF + 1;
  259. hns_mac_reset(vf_cb->mac_cb);
  260. hns_ppe_reset_common(vf_cb->dsaf_dev, ppe_common_index);
  261. }
  262. }
  263. void hns_ae_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
  264. {
  265. u32 flag;
  266. if (is_tx_ring(ring))
  267. flag = RCB_INT_FLAG_TX;
  268. else
  269. flag = RCB_INT_FLAG_RX;
  270. hns_rcb_int_ctrl_hw(ring->q, flag, mask);
  271. }
  272. static void hns_aev2_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
  273. {
  274. u32 flag;
  275. if (is_tx_ring(ring))
  276. flag = RCB_INT_FLAG_TX;
  277. else
  278. flag = RCB_INT_FLAG_RX;
  279. hns_rcbv2_int_ctrl_hw(ring->q, flag, mask);
  280. }
  281. static void hns_ae_toggle_queue_status(struct hnae_queue *queue, u32 val)
  282. {
  283. struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(queue->dev);
  284. if (AE_IS_VER1(dsaf_dev->dsaf_ver))
  285. hns_rcb_int_clr_hw(queue, RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
  286. else
  287. hns_rcbv2_int_clr_hw(queue, RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
  288. hns_rcb_start(queue, val);
  289. }
  290. static int hns_ae_get_link_status(struct hnae_handle *handle)
  291. {
  292. u32 link_status;
  293. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  294. hns_mac_get_link_status(mac_cb, &link_status);
  295. return !!link_status;
  296. }
  297. static int hns_ae_get_mac_info(struct hnae_handle *handle,
  298. u8 *auto_neg, u16 *speed, u8 *duplex)
  299. {
  300. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  301. return hns_mac_get_port_info(mac_cb, auto_neg, speed, duplex);
  302. }
  303. static void hns_ae_adjust_link(struct hnae_handle *handle, int speed,
  304. int duplex)
  305. {
  306. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  307. hns_mac_adjust_link(mac_cb, speed, duplex);
  308. }
  309. static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
  310. u32 *uplimit)
  311. {
  312. *uplimit = HNS_RCB_RING_MAX_PENDING_BD;
  313. }
  314. static void hns_ae_get_pauseparam(struct hnae_handle *handle,
  315. u32 *auto_neg, u32 *rx_en, u32 *tx_en)
  316. {
  317. assert(handle);
  318. hns_mac_get_autoneg(hns_get_mac_cb(handle), auto_neg);
  319. hns_mac_get_pauseparam(hns_get_mac_cb(handle), rx_en, tx_en);
  320. }
  321. static int hns_ae_set_autoneg(struct hnae_handle *handle, u8 enable)
  322. {
  323. assert(handle);
  324. return hns_mac_set_autoneg(hns_get_mac_cb(handle), enable);
  325. }
  326. static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
  327. {
  328. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  329. hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
  330. hns_mac_set_promisc(mac_cb, (u8)!!en);
  331. }
  332. static int hns_ae_get_autoneg(struct hnae_handle *handle)
  333. {
  334. u32 auto_neg;
  335. assert(handle);
  336. hns_mac_get_autoneg(hns_get_mac_cb(handle), &auto_neg);
  337. return auto_neg;
  338. }
  339. static int hns_ae_set_pauseparam(struct hnae_handle *handle,
  340. u32 autoneg, u32 rx_en, u32 tx_en)
  341. {
  342. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  343. int ret;
  344. ret = hns_mac_set_autoneg(mac_cb, autoneg);
  345. if (ret)
  346. return ret;
  347. return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
  348. }
  349. static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
  350. u32 *tx_usecs, u32 *rx_usecs)
  351. {
  352. struct ring_pair_cb *ring_pair =
  353. container_of(handle->qs[0], struct ring_pair_cb, q);
  354. *tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
  355. ring_pair->port_id_in_comm);
  356. *rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
  357. ring_pair->port_id_in_comm);
  358. }
  359. static void hns_ae_get_rx_max_coalesced_frames(struct hnae_handle *handle,
  360. u32 *tx_frames, u32 *rx_frames)
  361. {
  362. struct ring_pair_cb *ring_pair =
  363. container_of(handle->qs[0], struct ring_pair_cb, q);
  364. *tx_frames = hns_rcb_get_coalesced_frames(ring_pair->rcb_common,
  365. ring_pair->port_id_in_comm);
  366. *rx_frames = hns_rcb_get_coalesced_frames(ring_pair->rcb_common,
  367. ring_pair->port_id_in_comm);
  368. }
  369. static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
  370. u32 timeout)
  371. {
  372. struct ring_pair_cb *ring_pair =
  373. container_of(handle->qs[0], struct ring_pair_cb, q);
  374. return hns_rcb_set_coalesce_usecs(
  375. ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
  376. }
  377. static int hns_ae_set_coalesce_frames(struct hnae_handle *handle,
  378. u32 coalesce_frames)
  379. {
  380. struct ring_pair_cb *ring_pair =
  381. container_of(handle->qs[0], struct ring_pair_cb, q);
  382. return hns_rcb_set_coalesced_frames(
  383. ring_pair->rcb_common,
  384. ring_pair->port_id_in_comm, coalesce_frames);
  385. }
  386. void hns_ae_update_stats(struct hnae_handle *handle,
  387. struct net_device_stats *net_stats)
  388. {
  389. int port;
  390. int idx;
  391. struct dsaf_device *dsaf_dev;
  392. struct hns_mac_cb *mac_cb;
  393. struct hns_ppe_cb *ppe_cb;
  394. struct hnae_queue *queue;
  395. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  396. u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
  397. u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
  398. u64 rx_missed_errors = 0;
  399. dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
  400. if (!dsaf_dev)
  401. return;
  402. port = vf_cb->port_index;
  403. ppe_cb = hns_get_ppe_cb(handle);
  404. mac_cb = hns_get_mac_cb(handle);
  405. for (idx = 0; idx < handle->q_num; idx++) {
  406. queue = handle->qs[idx];
  407. hns_rcb_update_stats(queue);
  408. tx_bytes += queue->tx_ring.stats.tx_bytes;
  409. tx_packets += queue->tx_ring.stats.tx_pkts;
  410. rx_bytes += queue->rx_ring.stats.rx_bytes;
  411. rx_packets += queue->rx_ring.stats.rx_pkts;
  412. rx_errors += queue->rx_ring.stats.err_pkt_len
  413. + queue->rx_ring.stats.l2_err
  414. + queue->rx_ring.stats.l3l4_csum_err;
  415. }
  416. hns_ppe_update_stats(ppe_cb);
  417. rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
  418. tx_errors += ppe_cb->hw_stats.tx_err_checksum
  419. + ppe_cb->hw_stats.tx_err_fifo_empty;
  420. if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
  421. hns_dsaf_update_stats(dsaf_dev, port);
  422. /* for port upline direction, i.e., rx. */
  423. rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
  424. rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
  425. rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
  426. /* for port downline direction, i.e., tx. */
  427. port = port + DSAF_PPE_INODE_BASE;
  428. hns_dsaf_update_stats(dsaf_dev, port);
  429. tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
  430. tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
  431. tx_dropped += dsaf_dev->hw_stats[port].crc_false;
  432. tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
  433. tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
  434. tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
  435. }
  436. hns_mac_update_stats(mac_cb);
  437. rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
  438. tx_errors += mac_cb->hw_stats.tx_bad_pkts
  439. + mac_cb->hw_stats.tx_fragment_err
  440. + mac_cb->hw_stats.tx_jabber_err
  441. + mac_cb->hw_stats.tx_underrun_err
  442. + mac_cb->hw_stats.tx_crc_err;
  443. net_stats->tx_bytes = tx_bytes;
  444. net_stats->tx_packets = tx_packets;
  445. net_stats->rx_bytes = rx_bytes;
  446. net_stats->rx_dropped = 0;
  447. net_stats->rx_packets = rx_packets;
  448. net_stats->rx_errors = rx_errors;
  449. net_stats->tx_errors = tx_errors;
  450. net_stats->tx_dropped = tx_dropped;
  451. net_stats->rx_missed_errors = rx_missed_errors;
  452. net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
  453. net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
  454. net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
  455. net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
  456. net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
  457. }
  458. void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
  459. {
  460. int idx;
  461. struct hns_mac_cb *mac_cb;
  462. struct hns_ppe_cb *ppe_cb;
  463. u64 *p = data;
  464. struct hnae_vf_cb *vf_cb;
  465. if (!handle || !data) {
  466. pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
  467. return;
  468. }
  469. vf_cb = hns_ae_get_vf_cb(handle);
  470. mac_cb = hns_get_mac_cb(handle);
  471. ppe_cb = hns_get_ppe_cb(handle);
  472. for (idx = 0; idx < handle->q_num; idx++) {
  473. hns_rcb_get_stats(handle->qs[idx], p);
  474. p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
  475. }
  476. hns_ppe_get_stats(ppe_cb, p);
  477. p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
  478. hns_mac_get_stats(mac_cb, p);
  479. p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
  480. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  481. hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
  482. }
  483. void hns_ae_get_strings(struct hnae_handle *handle,
  484. u32 stringset, u8 *data)
  485. {
  486. int port;
  487. int idx;
  488. struct hns_mac_cb *mac_cb;
  489. struct hns_ppe_cb *ppe_cb;
  490. u8 *p = data;
  491. struct hnae_vf_cb *vf_cb;
  492. assert(handle);
  493. vf_cb = hns_ae_get_vf_cb(handle);
  494. port = vf_cb->port_index;
  495. mac_cb = hns_get_mac_cb(handle);
  496. ppe_cb = hns_get_ppe_cb(handle);
  497. for (idx = 0; idx < handle->q_num; idx++) {
  498. hns_rcb_get_strings(stringset, p, idx);
  499. p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
  500. }
  501. hns_ppe_get_strings(ppe_cb, stringset, p);
  502. p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
  503. hns_mac_get_strings(mac_cb, stringset, p);
  504. p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
  505. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  506. hns_dsaf_get_strings(stringset, p, port);
  507. }
  508. int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
  509. {
  510. u32 sset_count = 0;
  511. struct hns_mac_cb *mac_cb;
  512. assert(handle);
  513. mac_cb = hns_get_mac_cb(handle);
  514. sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
  515. sset_count += hns_ppe_get_sset_count(stringset);
  516. sset_count += hns_mac_get_sset_count(mac_cb, stringset);
  517. if (mac_cb->mac_type == HNAE_PORT_SERVICE)
  518. sset_count += hns_dsaf_get_sset_count(stringset);
  519. return sset_count;
  520. }
  521. static int hns_ae_config_loopback(struct hnae_handle *handle,
  522. enum hnae_loop loop, int en)
  523. {
  524. int ret;
  525. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  526. struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
  527. switch (loop) {
  528. case MAC_INTERNALLOOP_PHY:
  529. ret = 0;
  530. break;
  531. case MAC_INTERNALLOOP_SERDES:
  532. ret = hns_mac_config_sds_loopback(vf_cb->mac_cb, en);
  533. break;
  534. case MAC_INTERNALLOOP_MAC:
  535. ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
  536. break;
  537. default:
  538. ret = -EINVAL;
  539. }
  540. if (!ret)
  541. hns_dsaf_set_inner_lb(mac_cb->dsaf_dev, mac_cb->mac_id, en);
  542. return ret;
  543. }
  544. void hns_ae_update_led_status(struct hnae_handle *handle)
  545. {
  546. struct hns_mac_cb *mac_cb;
  547. assert(handle);
  548. mac_cb = hns_get_mac_cb(handle);
  549. if (!mac_cb->cpld_vaddr)
  550. return;
  551. hns_set_led_opt(mac_cb);
  552. }
  553. int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
  554. enum hnae_led_state status)
  555. {
  556. struct hns_mac_cb *mac_cb;
  557. assert(handle);
  558. mac_cb = hns_get_mac_cb(handle);
  559. return hns_cpld_led_set_id(mac_cb, status);
  560. }
  561. void hns_ae_get_regs(struct hnae_handle *handle, void *data)
  562. {
  563. u32 *p = data;
  564. u32 rcb_com_idx;
  565. int i;
  566. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  567. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  568. hns_ppe_get_regs(ppe_cb, p);
  569. p += hns_ppe_get_regs_count();
  570. rcb_com_idx = hns_dsaf_get_comm_idx_by_port(vf_cb->port_index);
  571. hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[rcb_com_idx], p);
  572. p += hns_rcb_get_common_regs_count();
  573. for (i = 0; i < handle->q_num; i++) {
  574. hns_rcb_get_ring_regs(handle->qs[i], p);
  575. p += hns_rcb_get_ring_regs_count();
  576. }
  577. hns_mac_get_regs(vf_cb->mac_cb, p);
  578. p += hns_mac_get_regs_count(vf_cb->mac_cb);
  579. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  580. hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
  581. }
  582. int hns_ae_get_regs_len(struct hnae_handle *handle)
  583. {
  584. u32 total_num;
  585. struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  586. total_num = hns_ppe_get_regs_count();
  587. total_num += hns_rcb_get_common_regs_count();
  588. total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
  589. total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
  590. if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
  591. total_num += hns_dsaf_get_regs_count();
  592. return total_num;
  593. }
  594. static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
  595. {
  596. return HNS_PPEV2_RSS_KEY_SIZE;
  597. }
  598. static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
  599. {
  600. return HNS_PPEV2_RSS_IND_TBL_SIZE;
  601. }
  602. static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
  603. u8 *hfunc)
  604. {
  605. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  606. /* currently we support only one type of hash function i.e. Toep hash */
  607. if (hfunc)
  608. *hfunc = ETH_RSS_HASH_TOP;
  609. /* get the RSS Key required by the user */
  610. if (key)
  611. memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
  612. /* update the current hash->queue mappings from the shadow RSS table */
  613. memcpy(indir, ppe_cb->rss_indir_table,
  614. HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
  615. return 0;
  616. }
  617. static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
  618. const u8 *key, const u8 hfunc)
  619. {
  620. struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
  621. /* set the RSS Hash Key if specififed by the user */
  622. if (key)
  623. hns_ppe_set_rss_key(ppe_cb, (u32 *)key);
  624. /* update the shadow RSS table with user specified qids */
  625. memcpy(ppe_cb->rss_indir_table, indir,
  626. HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
  627. /* now update the hardware */
  628. hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
  629. return 0;
  630. }
  631. static struct hnae_ae_ops hns_dsaf_ops = {
  632. .get_handle = hns_ae_get_handle,
  633. .put_handle = hns_ae_put_handle,
  634. .init_queue = hns_ae_init_queue,
  635. .fini_queue = hns_ae_fini_queue,
  636. .start = hns_ae_start,
  637. .stop = hns_ae_stop,
  638. .reset = hns_ae_reset,
  639. .toggle_ring_irq = hns_ae_toggle_ring_irq,
  640. .toggle_queue_status = hns_ae_toggle_queue_status,
  641. .get_status = hns_ae_get_link_status,
  642. .get_info = hns_ae_get_mac_info,
  643. .adjust_link = hns_ae_adjust_link,
  644. .set_loopback = hns_ae_config_loopback,
  645. .get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
  646. .get_pauseparam = hns_ae_get_pauseparam,
  647. .set_autoneg = hns_ae_set_autoneg,
  648. .get_autoneg = hns_ae_get_autoneg,
  649. .set_pauseparam = hns_ae_set_pauseparam,
  650. .get_coalesce_usecs = hns_ae_get_coalesce_usecs,
  651. .get_rx_max_coalesced_frames = hns_ae_get_rx_max_coalesced_frames,
  652. .set_coalesce_usecs = hns_ae_set_coalesce_usecs,
  653. .set_coalesce_frames = hns_ae_set_coalesce_frames,
  654. .set_promisc_mode = hns_ae_set_promisc_mode,
  655. .set_mac_addr = hns_ae_set_mac_address,
  656. .set_mc_addr = hns_ae_set_multicast_one,
  657. .set_mtu = hns_ae_set_mtu,
  658. .update_stats = hns_ae_update_stats,
  659. .set_tso_stats = hns_ae_set_tso_stats,
  660. .get_stats = hns_ae_get_stats,
  661. .get_strings = hns_ae_get_strings,
  662. .get_sset_count = hns_ae_get_sset_count,
  663. .update_led_status = hns_ae_update_led_status,
  664. .set_led_id = hns_ae_cpld_set_led_id,
  665. .get_regs = hns_ae_get_regs,
  666. .get_regs_len = hns_ae_get_regs_len,
  667. .get_rss_key_size = hns_ae_get_rss_key_size,
  668. .get_rss_indir_size = hns_ae_get_rss_indir_size,
  669. .get_rss = hns_ae_get_rss,
  670. .set_rss = hns_ae_set_rss
  671. };
  672. int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
  673. {
  674. struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
  675. static atomic_t id = ATOMIC_INIT(-1);
  676. switch (dsaf_dev->dsaf_ver) {
  677. case AE_VERSION_1:
  678. hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
  679. break;
  680. case AE_VERSION_2:
  681. hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
  682. break;
  683. default:
  684. break;
  685. }
  686. snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
  687. (int)atomic_inc_return(&id));
  688. ae_dev->ops = &hns_dsaf_ops;
  689. ae_dev->dev = dsaf_dev->dev;
  690. return hnae_ae_register(ae_dev, THIS_MODULE);
  691. }
  692. void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
  693. {
  694. hnae_ae_unregister(&dsaf_dev->ae_dev);
  695. }