hns_ae_adapt.c 25 KB

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