hns_ae_adapt.c 23 KB

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